iOS 多张图片保存到相册问题(add multiple images to photo album)

时间:2022-05-22 22:56:19

  不知道朋友们有木有做过多图保存到系统的相册这个需求,我在用`UIImageWriteToSavedPhotosAlbum`保存图片时候,在代理回调方`didFinishSavingWithError`中有些图片会出现这样的错误:

  iOS 多张图片保存到相册问题(add multiple images to photo album)

  

  原因上面写的很清楚,在同时保存多张图的时候,系统资源有限,来不及处理全部图片,容易出现写入错误。如果每次保存的时候一张保存完再保存另一张,就不会出现这个错误了。

  其实管理相册的是`ALAssetsLibrary`这个类,苹果官方对它的描述是这样的:An instance of ALAssetsLibrary provides access to the videos and photos that are under the control of the Photos application. `ALAssetsLibrary`中提供了保存到相册的API:

 // With a UIImage, the API user can use -[UIImage CGImage] to get a CGImageRef, and cast -[UIImage imageOrientation] to ALAssetOrientation.
- (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"); // The API user will have to specify the orientation key in the metadata dictionary to preserve the orientation of the image
- (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"); // If there is a conflict between the metadata in the image data and the metadata dictionary, the image data metadata values will be overwritten
- (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");

后面两个需要提供图片的metadata, 我用的是第一个方法。下面上码~

1. 初始化一个全局的`ALAssetsLibrary`

ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];

2.封装一个方法,把需要保存的图片放在一个数组中,每次取第一张,调用`writeImageToSavedPhotosAlbum`保存,如果保存成功,则将其移出数组,再保存下一张,如果有错误,我这边是用一个bool变量`isImagesSavedFailed`来记录(我这边需要图片全部保存成功)。
 typedef void (^completion_t)(id result);

 - (void) writeImages:(NSMutableArray*)images
completion:(completion_t)completionHandler {
if ([images count] == ) {
if (completionHandler) {
// Signal completion to the call-site. Use an appropriate result,
// instead of @"finished" possibly pass an array of URLs and NSErrors
// generated below in "handle URL or error".
completionHandler(@"images are all saved.");
}
return;
}
UIImage* image = [images firstObject];
[assetsLibrary writeImageToSavedPhotosAlbum:image.CGImage
orientation:ALAssetOrientationUp
completionBlock:^(NSURL *assetURL, NSError *error){
// Caution: check the execution context - it may be any thread,
// possibly use dispatch_async to dispatch to the main thread or
// any other queue.
// handle URL or error
if (error) {
NSLog(@"error = %@", [error localizedDescription]);
isImagesSavedFailed = true;
return;
}
[images removeObjectAtIndex:];
// next image:
[self writeImages:images completion:completionHandler];
}];
}

3. 调用示例

 NSMuatableArray *saveImages;//保存到相册的图片
[self writeImages:saveImages completion:^(id result) {
NSLog(@"Result: %@", result);
[hud stopLoading];
if (isImagesSavedFailed) {
//handle failed.
}else{
//handle success.
}
}];

参考链接 :http://*.com/questions/20662908/ios-programming-using-threads-to-add-multiple-images-to-library

======================= 分割线 ===============================

好久木有写博客了,有很多东西木有记录,还是记录下印象深一点。