ios awkard 图像加载(url 到 nsdata 到 uiimage 到 uiimageview) [英] ios awkard image loading (url to nsdata to uiimage to uiimageview)

查看:57
本文介绍了ios awkard 图像加载(url 到 nsdata 到 uiimage 到 uiimageview)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要你在这个问题中幽默我.这有点伪代码,因为实际情况相当复杂.除非需要,否则我不会以这种方式加载图像.假设我需要.

I need you to humor me during this question. This is somewhat pseudo code because the actual situation is quite complex. I wouldn't load an image this way unless I needed to. Assume that I need to.

NSURL *bgImageURL = [NSURL URLWithString:@"https://www.google.com/images/srpr/logo3w.png"];

UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:bgImageURL]];

[self.anIBOutletOfUIImageView setImage:img];

但是我用

-[__NSCFData _isResizable]: unrecognized selector sent to instance 0x9508c70

如何将 URL 中的图像加载到 NSData 中,然后将该 NSData 加载到 UIImage 中并将该 UIImage 设置为我的 UIImageView 的图像?

How can I load an image from a URL into NSData and then load that NSData into a UIImage and set that UIImage as the image for my UIImageView?

再一次,我意识到这听起来像是无稽之谈,但由于我使用的图像缓存系统,我必须以这种方式做事:(

Again, I realize this sounds like nonsense, but due to an image caching system I'm using I have to do things this way :(

推荐答案

我将此代码插入到新的 iPad单视图应用程序"模板中:

I plugged this code into a new, iPad "Single view application" template:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSURL *bgImageURL = [NSURL URLWithString:@"https://www.google.com/images/srpr/logo3w.png"];
    NSData *bgImageData = [NSData dataWithContentsOfURL:bgImageURL];
    UIImage *img = [UIImage imageWithData:bgImageData];
    [[self Imageview] setImage:img];
}

我可以正确加载图像.我什至为 UIImageView 尝试了不同的内容模式,它们都按预期工作.

I got the image to load up properly. I even tried different content modes for the UIImageView, and they all worked as expected.

如果你从头开始,你会遇到同样的问题吗?

If you start from scratch, do you get the same problem?

错误消息表明您正在向 NSData 对象发送_isResizable"消息.也许您无意中将 UIImageViewimage 属性设置为等于 NSData 而不是 UIImage.

The error message indicates that you're sending a message of "_isResizable" to an NSData object. Maybe you're inadvertently setting the UIImageView's image property equal to the NSData instead of the UIImage.

编辑/更新

我什至回去尝试了不同版本的单行"代码,看看是否有任何问题,因为我的工作"副本与您的非工作副本略有不同.我无法破解密码.我的猜测是有问题的代码没有损坏,但附近的东西是......

I even went back and tried different versions of "one-lining" the code to see if any of that mattered, since my "working" copy was slightly altered from your non-working copy. I couldn't get the code to break. My guess would be that the code in question isn't broken, but something nearby is...

这篇关于ios awkard 图像加载(url 到 nsdata 到 uiimage 到 uiimageview)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆