ALAssetsLibrary addAssetsGroupAlbumWithName无法在iOS 9

时间:2016-05-11 12:59:56

标签: ios objective-c iphone xcode alassetslibrary

我需要添加名称为" MyGroupName"在ALAssetsLibrary中。所以我使用了下面的代码。

 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)中总是为零。它的工作正常,组名为" MyGroupName"在iOS 9.3.1 iphone 5.我想知道为什么上面的代码不能在iphone 6上工作,是否有任何解决方案使它工作?

请帮帮我。提前致谢

2 个答案:

答案 0 :(得分:3)

1)首次导入

#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)现在创建将图像保存到自定义相册的方法

- (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 ”是您要保存的图像。

答案 1 :(得分:0)

请试试:

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/