ios 中图片上传并保存到沙盒中

//营业执照
- (IBAction)yinyezhizhao:(id)sender {
//选择图片来源
    self.yinyezhizhaoImg.tag=1;
    UIActionSheet*actionSheet;
    //    判断是否支持相机
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        actionSheet=[[UIActionSheet alloc]initWithTitle:@"选择一张照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"立即拍照上传",@"从手机相册选取", nil];
    }else{
        actionSheet=[[UIActionSheet alloc]initWithTitle:@"选择一张照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册选择", nil];
    }
    actionSheet.tag=250;
    [actionSheet showFromTabBar:self.tabBarController.tabBar];
}

- (IBAction)shangpujs:(id)sender {
    self.yinyezhizhaoImg.tag=2;
    UIActionSheet*actionSheet;
    //    判断是否支持相机
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        actionSheet=[[UIActionSheet alloc]initWithTitle:@"选择一张照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"立即拍照上传",@"从手机相册选取", nil];
    }else{
        actionSheet=[[UIActionSheet alloc]initWithTitle:@"选择一张照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册选择", nil];
    }
    actionSheet.tag=250;
    [actionSheet showFromTabBar:self.tabBarController.tabBar];
}

- (IBAction)shengfenzheng:(id)sender {
    self.yinyezhizhaoImg.tag=3;
    UIActionSheet*actionSheet;
    //    判断是否支持相机
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        actionSheet=[[UIActionSheet alloc]initWithTitle:@"选择一张照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"立即拍照上传",@"从手机相册选取", nil];
    }else{
        actionSheet=[[UIActionSheet alloc]initWithTitle:@"选择一张照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册选择", nil];
    }
    actionSheet.tag=250;
    [actionSheet showFromTabBar:self.tabBarController.tabBar];
}

#pragma mark-actionsheet delegate   图片来源定义
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (actionSheet.tag==250) {
        NSUInteger sourceType = 0;
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            switch (buttonIndex) {
                case 0:
                    //拍照
                    sourceType=UIImagePickerControllerSourceTypeCamera;
                    break;
                case 1:
                    // 相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
                case 2:
                    //取消
                    return;
            }
        }else{
            
            if (buttonIndex == 0) {
                //从相册选择
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            } else {
                //取消
                return;
            }
        }
        //跳转到相机或相册
        UIImagePickerController*imageController=[[UIImagePickerController alloc]init];
        imageController.delegate=self;
        imageController.allowsEditing=YES;
        imageController.sourceType=sourceType;
        [self presentViewController:imageController animated:YES completion:^{
            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
        }];
        
    }
}

#pragma mark-相机的协议
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    UIImage*image=[info objectForKey:UIImagePickerControllerEditedImage];
    NSData*imageData=UIImageJPEGRepresentation(image, 0.0000001);
    image=[UIImage imageWithData:imageData];
    NSData *data;//转图片流
    if (self.yinyezhizhaoImg.tag==1) {
        [self.yinyezhizhaoImg setImage:image];
        [data writeToFile:[PATH stringByAppendingString:@"/PostImages/yinyezhizhaoImg.jpg"] atomically:YES];//将其命名存到沙盒中
    }
    else if(self.yinyezhizhaoImg.tag==2){
        [self.shangpu setImage:image];
        [data writeToFile:[PATH stringByAppendingFormat:@"/PostImages/shangpu.jpg"] atomically:YES];
    }else
    {
        [self.shengfenzheng setImage:image];
        [data writeToFile:[PATH stringByAppendingFormat:@"/PostImages/shengfenzheng.jpg"] atomically:YES];
    }
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值