ALAssetsLibrary addAssetsGroupAlbumWithName is not working on iOS 9

╄→гoц情女王★ 提交于 2019-12-13 07:01:46

问题


I need to add group with name "MyGroupName" in ALAssetsLibrary . So I have used below code.

 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);
    }];

but inside "enumerateGroupsWithTypes" , group "g" is always nil in iOS 9.3.1 (iphone 6). its working correctly and group created with name "MyGroupName" on iOS 9.3.1 iphone 5. I want to know why above code is not working on iphone 6 and is there any solution to make it work ?

Please help me. Thanks in advance


回答1:


1) First Import

#import <Photos/Photos.h>
#import <Photos/PHAsset.h>
#import <AssetsLibrary/AssetsLibrary.h>

2) Set property for ALAsset

@property (nonatomic, strong) ALAssetsLibrary* assetsLibrary;

3) Then allocate ALAsset library in your .m file

- (ALAssetsLibrary*)assetsLibrary
{
    if (!_assetsLibrary) {
    _assetsLibrary = [[ALAssetsLibrary alloc] init];
    [ALAssetsLibrary disableSharedPhotoStreamsSupport];
}
return _assetsLibrary;
}

4 ) Now create method for save image to custom album

- (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 ) Now call this method to save the image like

    [self saveImageDatas:myimage toAlbum:@"MyGroupName" withCompletionBlock:^(NSError *error) {
    if (!error) {
        NSLog(@"Sucess");
    }
}];

"myimage" is your image that you want to save.




回答2:


Please try this on:

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");
}];

Or Please check this link also

http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/



来源:https://stackoverflow.com/questions/37163476/alassetslibrary-addassetsgroupalbumwithname-is-not-working-on-ios-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!