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上添加一 ...
随机推荐
- 【原】spark-submit提交应用程序的内部流程
我们经常通过spark-submit来提交spark应用程序,那么让我们一起看一下这里面到底发生了什么吧. 知识点: 1.CLI命令行界面启动Spark应用程序 Unix有两种方式:1)spark-s ...
- vijosP1164 曹冲养猪
vijosP1164 曹冲养猪 链接:https://vijos.org/p/1164 [思路] 数学. 如果x不能满足模公式则+gcd,gcd=a的积(a互质)使加上gcd后依然满足前面的模公式. ...
- CSS書寫規範及CSS Hack
基本原则: CSS样式可细分为3类:自定义样式.重新定义HTML样式.链接状态样式. 样式为设计师自定义的新 CSS 样式,影响被使用本样式的区域,用于完成网页中局部的样式设定. 样式名 “.”+“相 ...
- Docker系列(一)安装
操作系统版本:Centos7 Docker版本:1.8 设置安装源 1 cat > /etc/yum.repos.d/docker.repo << -EOF 2 [dockerr ...
- 组合数学及其应用——格路径与Schroder数
格路径问题是探讨在如下所示中的一个格点图上,从(0,0)位置到达(p,q)所有可能的情况数.我们称这样的通路为一条格路径. 格点图: · · · · · · · · · · · · · · · · · ...
- POJ1811- Prime Test(Miller–Rabin+Pollard's rho)
题目大意 给你一个非常大的整数,判断它是不是素数,如果不是则输出它的最小的因子 题解 看了一整天<初等数论及其应用>相关部分,终于把Miller–Rabin和Pollard's rho这两 ...
- POJ2773 - Happy 2006(欧拉函数)
题目大意 给定两个数m,k,要求你求出第k个和m互质的数 题解 我们需要知道一个等式,gcd(a,b)=gcd(a+t*b,b) 证明如下:gcd(a+t*b,b)=gcd(b,(a+t*b)%b)= ...
- [读书笔记]算法(Sedgewick著)·第一章(1)
到家放松放松之后就开始学习算法了,手里拿的是拿的是一本Robert Sedgewick的橙皮书<算法(第四版)>的.这本书与导论那本书的不同之处在于轻数学思想.重实现,也就是说这是一本很不 ...
- HTML 网页游戏 2048
新手只会一点html和css,javascript基本不会,更别提jQuery了= = 跟着慕课网的教学视频(视频地址:http://www.imooc.com/learn/76)一点点做的,由于自己 ...
- What does enumerable mean?
I was directed to MDN's for..in page when it said, "for..in Iterates over the enumerable proper ...