我从库中获取ALAsset
,但当我尝试设置UIImageView
时,UIImage
为零。
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);
}];
知道我做错了什么?
答案 0 :(得分:1)
这样对我有用:
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 :(得分:1)
你的代码看起来很棒,这让我怀疑问题是你看不到的地方 - 也就是说,imageView
本身可能是零。这会导致imageView.image
为零,因此您可能会想到对[UIImage imageWithCGImage...]
的调用失败。但事实并非如此!
这里的道德是:更多地解开你的代码。你写了这个:
imageView.image = [UIImage imageWithCGImage:representation.fullResolutionImage
scale:[representation scale]
orientation:(UIImageOrientation)[representation orientation]];
这就是隐藏真正问题的原因。如果你只写了这个:
UIImage* image = [UIImage imageWithCGImage:representation.fullResolutionImage
scale:[representation scale]
orientation:(UIImageOrientation)[representation orientation]];
NSLog(@"%@", image);
imageView.image = image;
// ...
...很明显你是从资产中成功获取图像,但是当你试图将它分配给图像视图时,图像视图就不能接收它了,那就是球落了下来。