我从库中获取一个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);
}];
知道我做错了什么吗?
发布于 2014-04-18 02:15:52
您的代码看起来很棒,这使我怀疑问题就在某个您没有注意到的地方--也就是说,也许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;
// ...
..。很明显,您已经成功地从资产中获取了图像,但是当您试图将它分配给图像视图时,图像视图并没有接收到它,这就是球被丢弃的地方。
发布于 2014-04-18 02:15:24
就像这个对我有用:
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;
}
https://stackoverflow.com/questions/23146202
复制相似问题