ALAssetsLibrary addAssetsGroupAlbumWithName 不适用于 iOS 9

Posted

技术标签:

【中文标题】ALAssetsLibrary addAssetsGroupAlbumWithName 不适用于 iOS 9【英文标题】:ALAssetsLibrary addAssetsGroupAlbumWithName is not working on iOS 9 【发布时间】:2016-05-11 12:59:55 【问题描述】:

我需要在 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 上创建了名为“MyGroupName”的组。我想知道为什么上面的代码在 iphone 6 上不起作用,有什么解决方案可以让它工作吗?

请帮助我。提前致谢

【问题讨论】:

请导入 Photos.framework。然后检查它会起作用 我添加了两个框架(照片和照片UI)并像#import #import 一样导入。但它仍然不起作用@VijayKachhadiya 你添加了 ALAssetsLibrary 框架吗? 是的,兄弟。添加和导入像 #import 仍然无法正常工作 我发布了一个答案,请尝试一下。我会为你工作 【参考方案1】:

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

【讨论】:

ALAssetsLibrary 在 iOS9 中已被弃用,取而代之的是 PHPhotoLibrary。但是将您的代码移植到新框架非常容易。谢谢!【参考方案2】:

请试穿一下:

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/

【讨论】:

感谢您的代码 vijay。当我枚举资产库时它不起作用,值(此处为“g”)为零。我的代码 [libraryFolder enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *g, BOOL *stop) if ([[g valueForProperty:ALAssetsGroupPropertyName] isEqualToString:@"My Album"]) NSLog(@"yes folder created with name") ; failureBlock:^(NSError *error) NSLog(@"错误:添加文件夹"); ]; 它进入成功块。但未创建相册

以上是关于ALAssetsLibrary addAssetsGroupAlbumWithName 不适用于 iOS 9的主要内容,如果未能解决你的问题,请参考以下文章

ALAssetsLibrary 获取相机胶卷

保存图像元数据 PHPhotoLibrary 与 ALAssetsLibrary

不推荐使用 ALAssetsLibrary 方法

ALAssetsLibrary.enumerateGroupsWithTypes Swift 中的第一个参数

无法链接 ALAssetsLibrary

从 ALAssetsLibrary 获取所有照片