码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 相机上传图片给服务器

时间:2016-09-12 19:06:25      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

本人做iOS还不是很久,第一个遇到棘手的问题就是相机上传图片给服务器,明明相册上传一点问题都没有,可是相册每次都不行。

以下代码原理:既然相册可以,为什么相机不行,是因为得不到UIImagePickerControllerReferenceURL ,因为相机和相册所得到的info是不一样的,所以我为了得到这个UIImagePickerControllerReferenceURL键值,我把拍的照片用代码存到相册里面,再取出来用就可以啦。虽然这个方法有点傻,但是也费了我很长时间的。

不多说,贴代码。

 

//拍照
- (void)takePhoto {
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        NSLog(@"该设备不支持拍照,请你买新设备");
        return;
    }
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    imagePicker.delegate = self;
    //资源类型
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    //是否允许编辑
    imagePicker.allowsEditing = YES;
    
    [self presentViewController:imagePicker animated:YES completion:nil];
}

 

 然后支持协议UIImagePickerControllerDelegate和UINavigationControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    UIImage *image = [[UIImage alloc]init];
    NSURL *imageURL = [[NSURL alloc]init];
    __block NSString *imageFileName;
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        image = [info objectForKey:UIImagePickerControllerOriginalImage];
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error) {
            if (error) {
                NSLog(@"%@",error.localizedDescription);
            }else {
                NSLog(@"assetURL %@",assetURL);
                ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
                {
                    
                    ALAssetRepresentation *representation = [myasset defaultRepresentation];
                    
                    imageFileName = [representation filename];
                    NSLog(@"imageFileName=%@",imageFileName);
                    NSData *imgData = UIImageJPEGRepresentation(image,0);
                    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
                    [manager POST:getImagePathUrls parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
                        if (imgData!= nil) {
                            [formData appendPartWithFileData:imgData name:@"upload_file" fileName:imageFileName mimeType:@"image/*"];
                        }
                    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                        NSLog(@"%@",responseObject);
                        NSString *string = responseObject[@"alldata"];
                        responseObject = [[EncryptionTools sharedEncryptionTools]decryptString:string keyString:@"d2VuZGFjcC4zMTU4LsmNus" iv:nil];
                        NSDictionary *dic = [self jsonStringToObject:responseObject];
                        NSString *imgPath = dic[@"data"][@"path"];
                        [self.requestImgArr addObject:imgPath];
                        [self.tableView reloadData];
    
                       //在这里可以把刚存的图片删除
                    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                        NSLog(@"%@",error.localizedDescription);
                    }];
                };
                ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
                [assetslibrary assetForURL:assetURL
                               resultBlock:resultblock
                              failureBlock:nil];
            }
            
        }];
    }
      [picker dismissViewControllerAnimated:YES completion:nil];
      [self.tableView reloadData];
}

 写的不是很明白,希望对你有点帮助

 

iOS 相机上传图片给服务器

标签:

原文地址:http://www.cnblogs.com/Mrs-park/p/5865579.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!