iOS 多张图片保存到相册问题(add multiple images to photo album)
不知道朋友们有木有做过多图保存到系统的相册这个需求,我在用`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.
}
}];
======================= 分割线 ===============================
好久木有写博客了,有很多东西木有记录,还是记录下印象深一点。
iOS 多张图片保存到相册问题(add multiple images to photo album)的更多相关文章
- iOS ----------将照片保存到相册
在使用前 请导入photos.framework 然后导入 #import <Photos/PHPhotoLibrary.h> #import <Photos/PHAssetCha ...
- iOS截屏保存至相册
#pragma mark 截屏并保存至相册 -(void)screenShotsComplete:(void(^)(UIImage * img)) complete { CGSize imageSiz ...
- IOS 截屏(保存到相册中)
@interface NJViewController () /** * 点击截屏按钮 */ - (IBAction)captureView:(UIButton *)sender; /** * 白色v ...
- iOS开发之保存照片到系统相册(Photo Album)
iOS开发之保存照片到系统相册(Photo Album) 保存照片到系统相册这个功能很多社交类的APP都有的,今天我们简单讲解一下,如何将图片保存到系统相册(Photo Album). 创建UIIma ...
- [RN] React Native 图片保存到相册(支持 Android 和 ios)
React Native 图片保存到相册(支持 Android 和 ios) 原理: IOS用 RN自带的 CameraRoll, Android 使用 不成功,需要 react-native-fs ...
- iOS 拍照保存到相册
之前看了一些开源的代码,里面有一个功能,就是将图片下载到相册,仔细看了其中的代码,只有很简单的一句话,并且保存过后,还可以判断是否保存成功. 如下代码所示, 点击按钮,将self.imageView上 ...
- iOS开发之保存照片到自己创建的相簿
iOS开发之保存照片到自己创建的相簿 保存照片还可以用ALAssetsLibrary,ALAssetsLibrary提供了我们对iOS设备中的相片.视频的访问,是连接应用程序和相册之间访问的一个桥梁. ...
- Android--解决图片保存到相册显示1970年1月1日 8:00的问题
import android.content.Context; import android.content.Intent; import android.database.Cursor; impor ...
- iOSQuartz2D-04-手动剪裁图片并保存到相册
实现效果 操作步骤 绘制一个矩形框,弹出一个alertView,提示是否保存图片 点击"是",将图片保存到相册 在相册中查看保存的图片 效果图 实现思路 在控制器的view上添加一 ...
随机推荐
- 【JS】Intermediate2:Events and Callbacks
event-driven :waiting for and reacting to events 2.page loads, user interacts (clicks, hovers, chang ...
- Docker系列(一)安装
操作系统版本:Centos7 Docker版本:1.8 设置安装源 1 cat > /etc/yum.repos.d/docker.repo << -EOF 2 [dockerr ...
- centos6 kvm网卡桥接
以前用VMware,我的上司说,你既然都用CentOS的桌面,那就研究一下KVM. 好吧,上司做运维好几年了,就听了他的,装了一个KVM. KVM的网络默认是NAT,不方便,就学习BRIDGE!!! ...
- 100个常用的linux命令
1,echo “aa” > test.txt 和 echo “bb” >> test.txt //>将原文件清空,并且内容写入到文件中,>>将内容放到文件的尾部 2 ...
- UML类图中类与类的四种关系图解
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- 正尝试在 OS 载入程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内执行托管代码,这样做会导致应用程序挂起。
出错提示: 正尝试在 OS 载入程序锁内执行托管代码. 不要尝试在 DllMain 或映像初始化函数内执行托管代码,这样做会导致应用程序挂起. 原因分析: .NET2.0中添加了42种非常强大的调试助 ...
- SensorThread线程
SensorThread && createEventQueue http://www.csdn.com/html/itweb/20131101/200375.htm_123 htt ...
- MyPhone
based on H323plus project. https://github.com/muggot/myphone3/tree/master/myphone3
- JFinalo操作框架racle数据库
JFinal操作框架oracle数据库.在需求configPlugin()方法来配置链路oracle配置数据库 组态JFinal数据库操作窗口小部件,configPlugin方法 在这里,我打开jdb ...
- oracle pde文件导入
pde使用pl/sql developer的tools->import tables->pl/sql developer来导入