标签:
1 - (IBAction)save:(id)sender {
2 // 存储图片到"相机胶卷"
3 UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
4 }
5
6 // 成功保存图片到相册中, 必须调用此方法, 否则会报参数越界错误
7 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
8
9 }
1 - (void)viewDidLoad {
2 [super viewDidLoad];
3
4 UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://pic25.nipic.com/20121209/9252150_194258033000_2.jpg"]]];
5 [self saveImage:image];
6 }
7 - (void)saveImage:(UIImage*)image {
8 ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
9
10 [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error) {
11 if (error) {
12 NSLog(@"Save image fail:%@",error);
13 }else{
14 NSLog(@"Save image succeed.");
15 }
16 }];
17 }
Photos.framework框架非常强大,不止图片,自拍、全景、iOS9中新增的Live Photo、甚至视频等等,框架都有详细的划分和功能设置
在这里我要实现的是第一种方法实现不了的功能,那就是建一个属于这个App的相簿,这个功能确实会经常遇到,代码如下,有详细注释:
1 - (void)saveImage {
2 //保存图片
3 __block NSString *assetId = nil;
4 // 1. 存储图片到"相机胶卷"
5 [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
6 // 新建一个PHAssetCreationRequest对象
7 // 返回PHAsset(图片)的字符串标识
8 assetId = [PHAssetCreationRequest creationRequestForAssetFromImage:self.imageView.image].placeholderForCreatedAsset.localIdentifier;
9 } completionHandler:^(BOOL success, NSError * _Nullable error) {
10 // 2. 获得相册对象
11 PHAssetCollection *collection = [self getCollection];
12 // 3. 将“相机胶卷”中的图片添加到新的相册
13 [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
14 PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
15 NSLog(@"%@", [PHAsset fetchAssetsWithLocalIdentifiers:@[assetId] options:nil]);
16 // 根据唯一标示获得相片对象
17 PHAsset *asset = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetId] options:nil].firstObject;
18 // 添加图片到相册中
19 [request addAssets:@[asset]];
20 } completionHandler:^(BOOL success, NSError * _Nullable error) {
21 NSLog(@"成功保存到相簿:%@", collection.localizedTitle);
22 }];
23 }];
24 }
25
26 - (PHAssetCollection *)getCollection {
27 // 先获得之前创建过的相册
28 PHFetchResult<PHAssetCollection *> *collectionResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
29 for (PHAssetCollection *collection in collectionResult) {
30 if ([collection.localizedTitle isEqualToString:@"知乎日报"]) {
31 return collection;
32 }
33 }
34
35 // 如果相册不存在,就创建新的相册(文件夹)
36 __block NSString *collectionId = nil; // __block修改block外部的变量的值
37 // 这个方法会在相册创建完毕后才会返回
38 [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
39 // 新建一个PHAssertCollectionChangeRequest对象, 用来创建一个新的相册
40 collectionId = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"知乎日报"].placeholderForCreatedAssetCollection.localIdentifier;
41 } error:nil];
42
43 return [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[collectionId] options:nil].firstObject;
44 }
最后的话
Photos.framework框架确实非常强大,但在阅读Apple官方文档后,在使用时真的非常麻烦,在我看来,这个框架类似于Core Data,你的每一次操作都必须建立自己的Request,并在内存中建一个中间相簿进行缓冲,并且所有操作,系统都是在后台完成。
另外要注意的一点是:completionHandler:返回的闭包中不要进行前台视图的刷新操作!!!这是Apple文档中明确提到的。
标签:
原文地址:http://www.cnblogs.com/ritian/p/5441952.html