【问题标题】:Accessing custom photo album访问自定义相册
【发布时间】:2014-06-01 14:49:31
【问题描述】:

我的应用程序为用户创建独特的照片,然后将它们保存在自定义相册中:

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

当用户进入应用程序时,我想给他们一个自定义滑块,显示他们之前保存在“测试相册”中的照片。如何仅将“测试相册”中的照片作为 UIImages 获取,以便将它们展示给用户??

【问题讨论】:

    标签: ios uiimage image-gallery alassetslibrary


    【解决方案1】:

    1) 使用enumerateGroupsWithTypes:usingBlock:failureBlock: 枚举 ALAsset 库中的组。

    2) 检查群组的ALAssetsGroupPropertyName 是否与您使用valueForProperty: 搜索的专辑名称相同。

    例如

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

    3) 找到组后,使用enumerateAssetsUsingBlock: 枚举其中的资产。

    4) 对于每个ALAsset,检索它是defaultRepresentation,然后是fullScreenImage

    例如

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

    5) 对于每个ALAsset,使用ALAssetPropertyOrientationvalueForProperty: 检索它是UIImageOrientation

    例如

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

    6) 对于每个ALAsset alloc/init 一个UIImage 使用imageWithCGImage:scale:orientation:。然后,插入您在第 4 步和第 5 步中检索到的fullScreenImageUIImageOrientation

    例如

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

    7) 分配/初始化 UIImage 后,将其添加到 NSMutableArray

    8) 枚举完成后,将图像呈现给用户。

    注意:

    用于枚举组和资产的两种方法都是异步的,这意味着它们将立即返回。

    【讨论】:

    • 第一个示例将不胜感激。
    【解决方案2】:

    这很简单..我添加了我的代码。

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

    希望这对您有所帮助,....享受。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-03
      • 1970-01-01
      • 2012-08-31
      • 1970-01-01
      相关资源
      最近更新 更多