gpt4 book ai didi

ios - ALAsset 图像为空

转载 作者:行者123 更新时间:2023-11-28 22:10:00 24 4
gpt4 key购买 nike

我正在从库中获取 ALAsset,但是当我尝试设置 UIImageView 时,UIImage 为 nil。

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

知道我做错了什么吗?

最佳答案

您的代码看起来很棒,这让我怀疑问题出在您没有注意的地方 - 也就是说,imageView 本身可能是 nil。这会导致 imageView.image 为 nil,因此您认为对 [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;
// ...

... 很明显您已成功从 Assets 中获取图像,但是当您尝试将其分配给 ImageView 时, ImageView 不在那里接收它,这就是球掉了。

关于ios - ALAsset 图像为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23146202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com