【问题标题】:ALAssetsLibrary to get image from the custom album iosALAssetsLibrary 从自定义相册 ios 中获取图像
【发布时间】:2016-02-15 05:21:20
【问题描述】:

我正在使用 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 获取图像。我想一张一张地获取所有图像。

【问题讨论】:

    标签: ios objective-c iphone uiimagepickercontroller alassetslibrary


    【解决方案1】:

    试试这个代码。

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

    【讨论】:

    • 请看我的问题,我只想从我的专辑中得到,但是你通过'MuAlbum'名称来获取图像文件
    • 我正在使用github.com/rahulmane91/CustomAlbumDemo 示例,现在我添加了带有新操作的代码,但收到错误未定义的架构 armv7 符号:“_completion”,引用自:___30-[MTViewController getAction:]_block_invoke in MTViewController。 o ld:未找到架构 armv7 的符号
    【解决方案2】:

    你可以试试这个方法..

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

    然后像这样从数组中获取拇指图像。,

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

    就是这样。这项工作在 ios9..希望这有帮助..谢谢

    【讨论】:

      猜你喜欢
      • 2013-10-18
      • 2016-09-20
      • 2015-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多