二:在手机相册(ios设备相册)中创建相册
第一步:向工程里添加AssetsLibrary 库文件
第二步:在项目中"import" 导入库(在哪用就在哪导入)
?
1 |
#import <AssetsLibrary/AssetsLibrary.h> |
核心代码
在需要添加相册的地方调用代码
?
1 2 3 4 5 6 |
//添加到相册我让它执行是异步执行的方式,如果不想用这种方式,可以不去开一个线程 dispatch_async(dispatch_get_global_queue(0, 0), ^{ [self createAlbumInPhoneAlbum]; }); |
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
#pragma mark - 在手机相册中创建相册 - (void)createAlbumInPhoneAlbum { ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init]; NSMutableArray *groups=[[NSMutableArray alloc]init]; ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) { if (group) { [groups addObject:group]; } else { BOOL haveHDRGroup = NO; for (ALAssetsGroup *gp in groups) { NSString *name =[gp valueForProperty:ALAssetsGroupPropertyName]; if ([name isEqualToString:@"相册名字"]) { haveHDRGroup = YES; } } if (!haveHDRGroup) { //do add a group named "XXXX" [assetsLibrary addAssetsGroupAlbumWithName:@"相册名字" resultBlock:^(ALAssetsGroup *group) { [groups addObject:group]; } failureBlock:nil]; haveHDRGroup = YES; } } }; //创建相簿 [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:listGroupBlock failureBlock:nil]; [self saveToAlbumWithMetadata:nil imageData:UIImagePNGRepresentation(self.imageView.image) customAlbumName:@"相册名字" completionBlock:^ { //这里可以创建添加成功的方法 } failureBlock:^(NSError *error) { //处理添加失败的方法显示alert让它回到主线程执行,不然那个框框死活不肯弹出来 dispatch_async(dispatch_get_main_queue(), ^{ //添加失败一般是由用户不允许应用访问相册造成的,这边可以取出这种情况加以判断一下 if([error.localizedDescription rangeOfString:@"User denied access"].location != NSNotFound ||[error.localizedDescription rangeOfString:@"用户拒绝访问"].location!=NSNotFound){ UIAlertView *alert=[[UIAlertView alloc]initWithTitle:error.localizedDescription message:error.localizedFailureReason delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles: nil]; [alert show]; [alert release]; } }); }]; } - (void)saveToAlbumWithMetadata:(NSDictionary *)metadata imageData:(NSData *)imageData customAlbumName:(NSString *)customAlbumName completionBlock:(void (^)(void))completionBlock failureBlock:(void (^)(NSError *error))failureBlock { ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init]; void (^AddAsset)(ALAssetsLibrary *, NSURL *) = ^(ALAssetsLibrary *assetsLibrary, NSURL *assetURL) { [assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) { [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:customAlbumName]) { [group addAsset:asset]; if (completionBlock) { completionBlock(); } } } failureBlock:^(NSError *error) { if (failureBlock) { failureBlock(error); } }]; } failureBlock:^(NSError *error) { if (failureBlock) { failureBlock(error); } }]; }; [assetsLibrary writeImageDataToSavedPhotosAlbum:imageData metadata:metadata completionBlock:^(NSURL *assetURL, NSError *error) { if (customAlbumName) { [assetsLibrary addAssetsGroupAlbumWithName:customAlbumName resultBlock:^(ALAssetsGroup *group) { if (group) { [assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) { [group addAsset:asset]; if (completionBlock) { completionBlock(); } } failureBlock:^(NSError *error) { if (failureBlock) { failureBlock(error); } }]; } else { AddAsset(assetsLibrary, assetURL); } } failureBlock:^(NSError *error) { AddAsset(assetsLibrary, assetURL); }]; } else { if (completionBlock) { completionBlock(); } } }]; } |
三、ALAssetsLibrary、ALAssetsGroup、ALAsset 介绍
1、ALAssetsLibrary
ALAssetsLibrary提供了我们对iOS设备中的相片、视频的访问。它是连接了我们应用程序和相册之间的访问的一个桥梁。
2、ALAssetsGroup
指代一个相册。
3、ALAsset
每一个ALAsset代表一个单一资源文件(也就是一张图片,或者一个视频文件)
4、ALAssetRepresentation
ALAssetRepresentation封装了ALAsset,包含了一个资源文件中的很多属性。(可以说是ALAsset的不同的表示方式,本质上都表示同一个资源文件)