您的位置:首页 > 移动开发 > IOS开发

ios相机读取图片到相册writeImageToSavesPhotosAlbum出现的问题

2018-03-19 21:14 1421 查看
读取相机选择的照片 MImaLibTool *imgLibTool = [MImaLibTool shareMImaLibTool];
[imgLibTool.lib writeImageToSavedPhotosAlbum:[theImage CGImage] orientation:(ALAssetOrientation)[theImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
} else {
[imgLibTool.lib assetForURL:assetURL resultBlock:^(ALAsset *asset) {
if (asset) {

[_arrSelected addObject:asset];
[self finishSelectImg];
[picker dismissViewControllerAnimated:NO completion:nil];
}
} failureBlock:^(NSError *error) {

}];
}
}];


在获取图片路径时,由于图片过大经常可能会导致asset为空,最近在相机拍完照片完成获取图片时在代理回调方法didFinishSavingWithError中,不能及时获取到本地相册图片库中的数据,导致卡在相机界面的问题
其实管理相册的是`ALAssetsLibrary`这个类,苹果官方对它的描述是这样的:An instance of ALAssetsLibrary provides access to the videos and photos that are under the control of the Photos application. `ALAssetsLibrary`中提供了保存到相册的API,而且苹果官方已经不推荐使用:
1 // With a UIImage, the API user can use -[UIImage CGImage] to get a CGImageRef, and cast -[UIImage imageOrientation] to ALAssetOrientation.
2 - (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef orientation:(ALAssetOrientation)orientation completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock NS_DEPRECATED_IOS(4_0, 9_0, "Use creationRequestForAssetFromImage: on PHAssetChangeRequest from the Photos framework to create a new asset instead");
3
4 // The API user will have to specify the orientation key in the metadata dictionary to preserve the orientation of the image
5 - (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock NS_DEPRECATED_IOS(4_1, 9_0, "Use creationRequestForAssetFromImage: on PHAssetChangeRequest from the Photos framework to create a new asset instead");
6
7 // If there is a conflict between the metadata in the image data and the metadata dictionary, the image data metadata values will be overwritten
8 - (void)writeImageDataToSavedPhotosAlbum:(NSData *)imageData metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock NS_DEPRECATED_IOS(4_1, 9_0, "Use creationRequestForAssetFromImageData: on PHAssetChangeRequest from the Photos framework to create a new asset instead")那么TZImagePicker这个处理就比较好,根据系统版本来判断比较适合去调那个方法去保存和取出相册的图片,如果是原来的方法那么就需要多给系统0.5s的时间,让系统去更新相册的数据if (iOS8Later) {
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
if (iOS9Later) {
NSData *data = UIImageJPEGRepresentation(image, 0.9);
PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];
options.shouldMoveFile = YES;
PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAsset];
[request addResourceWithType:PHAssetResourceTypePhoto data:data options:options];
if (location) {
request.location = location;
}
request.creationDate = [NSDate date];
} else {
PHAssetChangeRequest *request = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
if (location) {
request.location = location;
}
request.creationDate = [NSDate date];
}
} completionHandler:^(BOOL success, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (success && completion) {
completion(nil);
} else if (error) {
NSLog(@"保存照片出错:%@",error.localizedDescription);
if (completion) {
completion(error);
}
}
});
}];
} else {
[self.assetLibrary writeImageToSavedPhotosAlbum:image.CGImage orientation:[self orientationFromImage:image] completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(@"保存图片失败:%@",error.localizedDescription);
if (completion) {
completion(error);
}
} else {
// 多给系统0.5秒的时间,让系统去更新相册数据
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (completion) {
completion(nil);
}
});
}
}];
}
部分图片和资料引用地址: http://www.cnblogs.com/A--G/p/5769502.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: