ALAssetsLibrary addAssetsGroupAlbumWithName在iOS 9上不起作用
我需要在ALAssetsLibrary中添加名为“MyGroupName”的组。 所以我使用了下面的代码。
ALAssetsLibrary * library = [[ALAssetsLibrary alloc] init]; __weak ALAssetsLibrary *lib = library; [library addAssetsGroupAlbumWithName:@"MyGroupName" resultBlock:^(ALAssetsGroup *group) { [lib enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *g, BOOL *stop) { if ([[g valueForProperty:ALAssetsGroupPropertyName] isEqualToString:@"MyGroupName"]) { NSLog(@"group created with name 'MyGroupName'"); } }failureBlock:^(NSError *error){ NSLog(@"failure %@",error); } ]; } failureBlock:^(NSError *error) { NSLog(@"failure %@",error); }];
但在“enumerateGroupsWithTypes”中,组“g”在iOS 9.3.1(iphone 6)中始终为零。 它的工作正常,并在iOS 9.3.1 iphone 5上创build名称为“MyGroupName”的组。我想知道为什么上面的代码不能在iPhone 6上工作,是否有任何解决scheme,使其工作?
请帮帮我。 提前致谢
1)第一次import
#import <Photos/Photos.h> #import <Photos/PHAsset.h> #import <AssetsLibrary/AssetsLibrary.h>
2)为ALAsset设置属性
@property (nonatomic, strong) ALAssetsLibrary* assetsLibrary;
3)然后在.m文件中分配ALAsset库
- (ALAssetsLibrary*)assetsLibrary { if (!_assetsLibrary) { _assetsLibrary = [[ALAssetsLibrary alloc] init]; [ALAssetsLibrary disableSharedPhotoStreamsSupport]; } return _assetsLibrary; }
4)现在创build保存图像到自定义相册的方法
- (void)saveImageDatas:(UIImage*)imageDatas toAlbum:(NSString*)album withCompletionBlock:(void(^)(NSError *error))block { ` if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ NSMutableArray* assets = [[NSMutableArray alloc]init]; PHAssetChangeRequest* assetRequest; @autoreleasepool { assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:imageDatas]; [assets addObject:assetRequest.placeholderForCreatedAsset]; } __block PHAssetCollectionChangeRequest* assetCollectionRequest = nil; PHFetchResult* result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil]; [result enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { PHAssetCollection* collection = (PHAssetCollection*)obj; if ([collection isKindOfClass:[PHAssetCollection class]]) { if ([[collection localizedTitle] isEqualToString:album]) { assetCollectionRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection]; [assetCollectionRequest addAssets:assets]; *stop = YES; } } }]; if (assetCollectionRequest == nil) { assetCollectionRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:album]; [assetCollectionRequest addAssets:assets]; } } completionHandler:^(BOOL success, NSError *error) { if (block) { block(error); } }]; } else { __weak ALAssetsLibrary* lib = [self assetsLibrary]; [[self assetsLibrary] writeImageDataToSavedPhotosAlbum:UIImageJPEGRepresentation(imageDatas, 1.0) metadata:nil completionBlock:^(NSURL* assetURL, NSError* error) { if (error != nil) { return; } __block BOOL albumWasFound = NO; [lib enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup* group, BOOL* stop) { if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:album]) { albumWasFound = YES; [lib assetForURL:assetURL resultBlock:^(ALAsset* asset){ [group addAsset:asset]; if (block) { block(nil); } }failureBlock:^(NSError* error) { if (block) { block(error); } }]; return; } if (group == nil && albumWasFound == NO) { [lib addAssetsGroupAlbumWithName:album resultBlock:^(ALAssetsGroup* group) { } failureBlock:^(NSError* error) { [lib assetForURL:assetURL resultBlock:^(ALAsset* asset){ [group addAsset:asset]; if (block) { block(nil); } }failureBlock:^(NSError* error) { if (block) { block(error); } }]; }]; } } failureBlock:^(NSError* error) { if (block) { block(error); } }]; }]; } }
5)现在调用这个方法来保存图像
[self saveImageDatas:myimage toAlbum:@"MyGroupName" withCompletionBlock:^(NSError *error) { if (!error) { NSLog(@"Sucess"); } }];
“ myimage ”是你想要保存的图像。
谢谢,,,我希望这会帮助你..如果这个答案是正确的,那么请投票给我..
请试试这个:
ALAssetsLibrary* libraryFolder = [[ALAssetsLibrary alloc] init]; [libraryFolder addAssetsGroupAlbumWithName:@"My Album" resultBlock:^(ALAssetsGroup *group) { NSLog(@"Adding Folder:'My Album', success: %s", group.editable ? "Success" : "Already created: Not Success"); } failureBlock:^(NSError *error) { NSLog(@"Error: Adding on Folder"); }];
或者请检查这个链接
http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/