iOS 相机上传图片给服务器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 相机上传图片给服务器相关的知识,希望对你有一定的参考价值。

本人做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 相机上传图片给服务器的主要内容,如果未能解决你的问题,请参考以下文章

IOS上传图片方向问题

H5调用本地相册/相机上传图片

将图像从库中上传到服务器

Xamarin 在 Webview 中使用相机上传图片

微信小程序excel添加图片

uni小程序上传图片,兼容安卓机不会自动弹出相机选项的写法