标签:
关于自定义相册删除复活的实现
1 - (void)getAllPhotos 2 { 3 ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 4 // 遍历所有的文件夹, 一个ALAssetsGroup对象就代表一个文件夹 5 [library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 6 // 遍历文件夹内的所有多媒体文件(图片、视频), 一个ALAsset对象就代表一张图片 7 [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { 8 // 缩略图 9 XMGLog(@"%@", [UIImage imageWithCGImage:result.thumbnail]); 10 // 获得原始图片 11 // XMGLog(@"%@", [UIImage imageWithCGImage:result.defaultRepresentation.fullResolutionImage]); 12 }]; 13 14 } failureBlock:nil]; 15 }
/** 相册库 */ @property (nonatomic, strong) ALAssetsLibrary *library;
1 - (ALAssetsLibrary *)library 2 { 3 if (!_library) { 4 _library = [[ALAssetsLibrary alloc] init]; 5 } 6 return _library; 7 } 8
1 - (IBAction)save 2 { 3 // 获得文件夹的名字 4 __block NSString *groupName = [self groupName]; 5 6 // self的弱引用 7 XMGWeakSelf; 8 9 // 图片库 10 __weak ALAssetsLibrary *weakLibrary = self.library; 11 12 // 创建文件夹 13 [weakLibrary addAssetsGroupAlbumWithName:groupName resultBlock:^(ALAssetsGroup *group) { 14 if (group) { // 新创建的文件夹 15 // 添加图片到文件夹中 16 [weakSelf addImageToGroup:group]; 17 } else { // 文件夹已经存在 18 [weakLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 19 NSString *name = [group valueForProperty:ALAssetsGroupPropertyName]; 20 if ([name isEqualToString:groupName]) { // 是自己创建的文件夹 21 // 添加图片到文件夹中 22 [weakSelf addImageToGroup:group]; 23 24 *stop = YES; // 停止遍历 25 } else if ([name isEqualToString:@"Camera Roll"]) { 26 // 文件夹被用户强制删除了 27 groupName = [groupName stringByAppendingString:@" "]; 28 // 存储新的名字 29 [[NSUserDefaults standardUserDefaults] setObject:groupName forKey:XMGGroupNameKey]; 30 [[NSUserDefaults standardUserDefaults] synchronize]; 31 // 创建新的文件夹 32 [weakLibrary addAssetsGroupAlbumWithName:groupName resultBlock:^(ALAssetsGroup *group) { 33 // 添加图片到文件夹中 34 [weakSelf addImageToGroup:group]; 35 } failureBlock:nil]; 36 } 37 } failureBlock:nil]; 38 } 39 } failureBlock:nil]; 40 } 41
1 /** 2 * 添加一张图片到某个文件夹中 3 */ 4 - (void)addImageToGroup:(ALAssetsGroup *)group 5 { 6 __weak ALAssetsLibrary *weakLibrary = self.library; 7 // 需要保存的图片 8 CGImageRef image = self.imageView.image.CGImage; 9 10 // 添加图片到【相机胶卷】 11 [weakLibrary writeImageToSavedPhotosAlbum:image metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) { 12 [weakLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) { 13 // 添加一张图片到自定义的文件夹中 14 [group addAsset:asset]; 15 [SVProgressHUD showSuccessWithStatus:@"保存成功!"]; 16 } failureBlock:nil]; 17 }]; 18 }
static NSString * const iCocosGroupNameKey = @"iCocosGroupNameKey"; static NSString * const iCocosDefaultGroupName = @"iCocos";
1 - (NSString *)groupName 2 { 3 // 先从沙盒中取得名字 4 NSString *groupName = [[NSUserDefaults standardUserDefaults] stringForKey:XMGGroupNameKey]; 5 if (groupName == nil) { // 沙盒中没有存储任何文件夹的名字 6 groupName = XMGDefaultGroupName; 7 8 // 存储名字到沙盒中 9 [[NSUserDefaults standardUserDefaults] setObject:groupName forKey:XMGGroupNameKey]; 10 [[NSUserDefaults standardUserDefaults] synchronize]; 11 } 12 13 return groupName; 14 } 15
标签:
原文地址:http://www.cnblogs.com/iCocos/p/4814541.html