【问题标题】:fetching photo alassetlibrary asset representation size zero获取照片alassetlibrary资产表示大小为零
【发布时间】:2012-07-31 02:43:12
【问题描述】:

当我使用 ALAssetLibrary 获取照片时,对于某些图像,我的 ImageView 上的 AssetRepresentation.size 为零,这不会生成图像。 代码如下:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

    if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqual:self.groupName]) {

        [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop){
            //Get the asset type
            NSString *assetType = [result valueForProperty:ALAssetPropertyType];
            if ([assetType isEqualToString:ALAssetTypePhoto]) {
                NSLog(@"Photo Asset");
            }
            //Get URLs for the assets
            NSDictionary *assetURLs = [result valueForProperty:ALAssetPropertyURLs];

            NSUInteger assetCounter = 0;
            for (NSString *assetURLKey in assetURLs) {
                assetCounter++;
            }
            //Get the asset's representation object
            ALAssetRepresentation *assetRepresentation = [result defaultRepresentation];

            //From this asset representation, we take out data for image and show it using imageview.

            dispatch_async(dispatch_get_main_queue(), ^(void){
                CGImageRef imgRef = [assetRepresentation fullResolutionImage];
                //Img Construction
                UIImage *image = [[[UIImage alloc] initWithCGImage:imgRef] autorelease];

                NSLog(@"before %@:::%lld", [image description], [assetRepresentation size]); //Prints '0' for size

                if((image != nil)&& [assetRepresentation size] != 0){

                   //display in image view
                }
                else{
                    // NSLog(@"Failed to load the image.");
                }
            });

        }];
    }
}failureBlock:^(NSError *error){
    NSLog(@"Error retrieving photos: %@", error);
}];


[library release];

请帮忙。我在这里做错了什么?我应该如何获取图像?

【问题讨论】:

    标签: iphone ios alassetslibrary alasset photolibrary


    【解决方案1】:

    我认为您发布资源库的时间太早了。

    【讨论】:

    • 我在枚举块之后发布了它。如果它发布得太早,它应该崩溃而不是显示大小为零的资产。不应该吗?
    【解决方案2】:

    乔·史密斯是对的!您过早释放库。 AssetLibrary 被释放的那一刻,所有的资产对象都将随之消失。而且由于这些枚举是块代码,所以在读取过程中会在某处执行AssetLibrary的释放。

    我建议您在应用程序委托中创建您的assetLibrary 以使其保持活动状态,并且仅在您从 ALAssetLibrary 收到更改通知 ALAssetsLibraryChangedNotification 时重置它

    【讨论】:

      猜你喜欢
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-20
      • 1970-01-01
      相关资源
      最近更新 更多