首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ALAsset图像为空

ALAsset图像为空
EN

Stack Overflow用户
提问于 2014-04-18 01:49:28
回答 2查看 924关注 0票数 0

我从库中获取一个ALAsset,但是当我试图设置一个UIImageView时,UIImage是零。

代码语言:javascript
运行
复制
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library assetForURL:[NSURL URLWithString:entityObject.localUrl] resultBlock:^(ALAsset *asset) {
        if (asset) {
            ALAssetRepresentation *representation = [asset defaultRepresentation];
            imageView.image = [UIImage imageWithCGImage:representation.fullResolutionImage
                                       scale:[representation scale]
                                 orientation:(UIImageOrientation)[representation orientation]];

            NSLog(@"imageView.image: %@",imageView.image); // imageView.image: (null)
            NSLog(@"image size %f", imageView.image.size.width); //image size: 0.000000
            imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, imageView.image.size.width, imageView.image.size.height);

        } else {
            NSLog(@"test not found?");
        }
    } failureBlock:^(NSError *error) {
        NSLog(@"FAILED TO FIND %@", error);
    }];

知道我做错了什么吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-18 02:15:52

您的代码看起来很棒,这使我怀疑问题就在某个您没有注意到的地方--也就是说,也许imageView本身就是零。这会导致imageView.image为零,因此您想象对[UIImage imageWithCGImage...]的调用失败了。但事实并非如此!

这里的寓意是:稍微解开你的代码。你写了这个:

代码语言:javascript
运行
复制
imageView.image = [UIImage imageWithCGImage:representation.fullResolutionImage
                                   scale:[representation scale]
                             orientation:(UIImageOrientation)[representation orientation]];

这才是真正的问题所在。如果你写了这篇文章:

代码语言:javascript
运行
复制
UIImage* image = [UIImage imageWithCGImage:representation.fullResolutionImage
                                   scale:[representation scale]
                             orientation:(UIImageOrientation)[representation orientation]];
NSLog(@"%@", image);
imageView.image = image;
// ...

..。很明显,您已经成功地从资产中获取了图像,但是当您试图将它分配给图像视图时,图像视图并没有接收到它,这就是球被丢弃的地方。

票数 1
EN

Stack Overflow用户

发布于 2014-04-18 02:15:24

就像这个对我有用:

代码语言:javascript
运行
复制
ALAssetsLibrary *library = [self defaultAssetsLibrary];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {

        if(result)
        {
            ALAssetRepresentation *representation = [result defaultRepresentation];
            imageView.image = [UIImage imageWithCGImage:representation.fullResolutionImage
                                                  scale:[representation scale]
                                            orientation:(UIImageOrientation)[representation orientation]];

            NSLog(@"imageView.image: %@",imageView.image); // imageView.image: (null)
            NSLog(@"image size %f", imageView.image.size.width); //image size: 0.000000
            imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, imageView.image.size.width, imageView.image.size.height);

        }
    }];

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

- (ALAssetsLibrary *)defaultAssetsLibrary {
        static dispatch_once_t pred = 0;
        static ALAssetsLibrary *library = nil;
        dispatch_once(&pred, ^{
            library = [[ALAssetsLibrary alloc] init];
        });
        return library;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23146202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档