简体   繁体   中英

Accessing custom photo album

My app creates unique photographs for the users then saves them in a custom photo album with this:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library saveImage:thisimage toAlbum:@"Test Album" withCompletionBlock:^(NSError *error) {
    if (error!=nil) {
        NSLog(@"Album save error: %@", [error description]);
    }
}];

When the user enters the application I want to give them a custom slider which shows the photos they saved in the "Test Album" previously. How do I get the photos only in the "Test Album" as UIImages so I can show them to the user??

1) Enumerate the groups in the ALAsset library using enumerateGroupsWithTypes:usingBlock:failureBlock: .

2) Check if the group's ALAssetsGroupPropertyName is equal to the album name that you are searching for using valueForProperty: .

Ex.

NSString * groupName = [group valueForProperty:ALAssetsGroupPropertyName];
[groupName localizedCaseInsensitiveCompare:albumName] == NSOrderedSame

3) Once you have found the group, enumerate the assets within it using enumerateAssetsUsingBlock: .

4) For every ALAsset retrieve it's defaultRepresentation and from that it's fullScreenImage .

Ex.

ALAssetRepresentation * assetRepresentation = [asset defaultRepresentation];
CGImageRef imageRef = [assetRepresentation fullScreenImage];

5) For every ALAsset retrieve it's UIImageOrientation using ALAssetPropertyOrientation and valueForProperty: .

Ex.

 UIImageOrientation imageOrientation = [[asset valueForProperty:ALAssetPropertyOrientation] intValue];

6) For every ALAsset alloc/init a UIImage using imageWithCGImage:scale:orientation: . Then, plug in the fullScreenImage and UIImageOrientation that you retrieved in the steps 4 and 5.

Ex.

UIImage * image = [UIImage imageWithCGImage:imageRef scale:1.0f orientation:imageOrientation];

7) Once you have alloc'd/init'd the UIImage add it in to a NSMutableArray .

8) Once the enumeration is complete, present the images to the user.

Note:

Both of the methods used to enumerate Groups and Assets are asynchronous meaning that they will return immediately.

It's Simple..I add my code.

    [_library enumerateGroupsWithTypes:ALAssetsGroupAll  usingBlock:^(ALAssetsGroup *group, BOOL *stop){

    if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqual:@"Test Album"]) {
        void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
        {
            if(result != nil)
            {
                [self.assets addObject:result]; //assets is NSMutableArray
            }
        };

            [group setAssetsFilter:[ALAssetsFilter allPhotos]];
            [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:assetEnumerator];
    }

    [self.collectionView reloadData];
}
                      failureBlock:^(NSError *error){

                          NSLog(@"failure"); }];}

Hope this is helpful ,....Enjoy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM