簡體   English   中英

ALAssetsLibrary從自定義相冊ios獲取圖像

[英]ALAssetsLibrary to get image from the custom album ios

我正在使用ALAssetsLibrary將圖像保存在應用程序的“海關相冊”中。 我能夠保存圖像,但無法從相冊中獲取圖像

__weak ALAssetsLibrary *lib = self.library;

[self.library addAssetsGroupAlbumWithName:@"MyAlbum" resultBlock:^(ALAssetsGroup *group) {

    ///checks if group previously created
    if(group == nil){

        //enumerate albums
        [lib enumerateGroupsWithTypes:ALAssetsGroupAlbum
                           usingBlock:^(ALAssetsGroup *g, BOOL *stop)
         {
             //if the album is equal to our album
             if ([[g valueForProperty:ALAssetsGroupPropertyName] isEqualToString:@"My Photo Album"]) {

                 //save image
                 [lib writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil
                                       completionBlock:^(NSURL *assetURL, NSError *error) {

                                           //then get the image asseturl
                                           [lib assetForURL:assetURL
                                                resultBlock:^(ALAsset *asset) {
                                                    //put it into our album
                                                    [g addAsset:asset];
                                                } failureBlock:^(NSError *error) {

                                                }];
                                       }];

             }
         }failureBlock:^(NSError *error){

         }];

    }else{
        // save image directly to library
        [lib writeImageDataToSavedPhotosAlbum:UIImagePNGRepresentation(image) metadata:nil
                              completionBlock:^(NSURL *assetURL, NSError *error) {

                                  [lib assetForURL:assetURL
                                       resultBlock:^(ALAsset *asset) {

                                           [group addAsset:asset];

                                       } failureBlock:^(NSError *error) {

                                       }];
                              }];
    }

} failureBlock:^(NSError *error) {

}];

如何從MyAlbum獲取圖像。 我想一張一張地獲取所有圖像。

試試這個代碼。

-(void)getImageVideosFromGallery:(void(^)(void))completion{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    NSString *albumName = [group valueForProperty:ALAssetsGroupPropertyName];
    if ([albumName isEqualToString:@"MyAlbum"]) {
        [group setAssetsFilter:[ALAssetsFilter allAssets]];
        [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {

            if (alAsset) {

                if ([[alAsset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
                    NSLog(@"Video");
                }
                else{
                    ALAssetRepresentation *representation = [alAsset defaultRepresentation];
                    UIImage  *imgThumb = [UIImage imageWithCGImage:[alAsset thumbnail]];
                    UIImage *imgOriginal = [UIImage imageWithCGImage:[[alAsset defaultRepresentation]fullScreenImage]];
                }
            }
        }];

        if (group == nil)
        {
            completion();
        }

    }
}failureBlock: ^(NSError *error) {
    NSLog(@"No groups");
}];
}

您可以嘗試這種方式。

-(void)loadlibraryImages
{
    NSMutableArray * _recentpictureArray=[[NSMutableArray alloc]init];
    ALAssetsLibrary *_library = [[ALAssetsLibrary alloc] init];



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

    if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqual:@"MyAlbum"]) {
        void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
        {
            if(result != nil)
            {
                [_recentpictureArray addObject:result];
            }
        };

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

    [self.collectionview reloadData]; // you can reload your collection view 
}
                      failureBlock:^(NSError *error){

                          NSLog(@"failure"); }];
NSLog(@"Count : %lu",(unsigned long)[_recentpictureArray count]);
}

然后像這樣從Array獲得拇指圖像。

    ALAsset *asset = [_recentpictureArray objectAtIndex:indexPath.row];
CGImageRef thumbnailImageRef = [asset aspectRatioThumbnail];
UIImage *thumbnail = [UIImage imageWithCGImage:thumbnailImageRef];
// Then set your image view's image.

而已。 在ios9上的這項工作。希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM