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

  

  

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

  其实管理相册的是`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://stackoverflow.com/questions/20662908/ios-programming-using-threads-to-add-multiple-images-to-library

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

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

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

  1. iOS ----------将照片保存到相册

    在使用前  请导入photos.framework 然后导入 #import <Photos/PHPhotoLibrary.h> #import <Photos/PHAssetCha ...

  2. iOS截屏保存至相册

    #pragma mark 截屏并保存至相册 -(void)screenShotsComplete:(void(^)(UIImage * img)) complete { CGSize imageSiz ...

  3. IOS 截屏(保存到相册中)

    @interface NJViewController () /** * 点击截屏按钮 */ - (IBAction)captureView:(UIButton *)sender; /** * 白色v ...

  4. iOS开发之保存照片到系统相册(Photo Album)

    iOS开发之保存照片到系统相册(Photo Album) 保存照片到系统相册这个功能很多社交类的APP都有的,今天我们简单讲解一下,如何将图片保存到系统相册(Photo Album). 创建UIIma ...

  5. [RN] React Native 图片保存到相册(支持 Android 和 ios)

    React Native 图片保存到相册(支持 Android 和 ios) 原理: IOS用 RN自带的 CameraRoll, Android 使用 不成功,需要 react-native-fs  ...

  6. iOS 拍照保存到相册

    之前看了一些开源的代码,里面有一个功能,就是将图片下载到相册,仔细看了其中的代码,只有很简单的一句话,并且保存过后,还可以判断是否保存成功. 如下代码所示, 点击按钮,将self.imageView上 ...

  7. iOS开发之保存照片到自己创建的相簿

    iOS开发之保存照片到自己创建的相簿 保存照片还可以用ALAssetsLibrary,ALAssetsLibrary提供了我们对iOS设备中的相片.视频的访问,是连接应用程序和相册之间访问的一个桥梁. ...

  8. Android--解决图片保存到相册显示1970年1月1日 8:00的问题

    import android.content.Context; import android.content.Intent; import android.database.Cursor; impor ...

  9. iOSQuartz2D-04-手动剪裁图片并保存到相册

    实现效果 操作步骤 绘制一个矩形框,弹出一个alertView,提示是否保存图片 点击"是",将图片保存到相册 在相册中查看保存的图片 效果图 实现思路 在控制器的view上添加一 ...

随机推荐

  1. leecode single numer

    http://www.acmerblog.com/leetcode-single-number-ii-5394.html acm之家的讲解总是那么到位 public class Solution { ...

  2. Storm系列(十五)架构分析之Executor-Spout

    Spout实现mk-threads接口用于创建与Executor对应的消息循环主函数. defmulti mk-threads executor-selector Mk-threads函数的主消息循环 ...

  3. 【原创】关于hashcode和equals的不同实现对HashMap和HashSet集合类的影响的探究

    这篇文章做了一个很好的测试:http://blog.csdn.net/afgasdg/article/details/6889383,判断往HashSet(不允许元素重复)里塞对象时,是如何判定set ...

  4. Fibonacci数列小程序

    Fibonacci数列小程序 问题分析:Fibonacci数列特征是前两项数均为1,从第三项起,前两项的和为第三项的数的数值用公式归纳起来为:f1=f2=1.f1=f1+f2.f2=f1+f2. 程序 ...

  5. PAT 1038 体验Python之美

    1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  6. UVALive 5111 Soccer Teams (动态规划)

    题意:给指定数量的数字“1”,“2”,“3”……,“9”.用所有这些数字加上任意个0组成一个数,要求数能被11整除,且数的位数尽量小. 能被11整除的数有一个特点,奇数位数字之和与偶数位之和的差为11 ...

  7. PC-如何提高计算机的启动和关机的速度?

    如何提高计算机的启动和关机的速度? 一.bios的优化设置 在bios设置的首页我们进入"advanced bios features"选项,将光标移到"frist bo ...

  8. __FILE__,__LINE__,FUNCTION__

    __FILE__,__LINE__,FUNCTION__实现代码跟踪调试 ( linux 下c语言编程 ) 先看下简单的初始代码:注意其编译运行后的结果. root@xuanfei-desktop:~ ...

  9. UML 2中结构图的介绍

    原文: http://www.ibm.com/developerworks/cn/rational/rationaledge/content/feb05/bell/ 这是关于统一建模语言.即UML 里 ...

  10. uva 11270 - Tiling Dominoes(插头dp)

    题目链接:uva 11270 - Tiling Dominoes 题目大意:用1∗2木块将给出的n∗m大小的矩阵填满的方法总数. 解题思路:插头dp的裸题,dp[i][s]表示第i块位置.而且该位置相 ...