简体   繁体   English

ios awkard图像加载(URL到nsdata到uiimage到uiimageview)

[英]ios awkard image loading (url to nsdata to uiimage to 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];

but I crash out with 但我崩溃了

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

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? 如何将URL中的图像加载到NSData中,然后将该NSData加载到UIImage中,并将该UIImage设置为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 :( 再次,我意识到这听起来像是胡说八道,但是由于使用了图像缓存系统,因此我必须以这种方式来做:(

How I usually handle this situation (not compiled, not tested): 我通常如何处理这种情况(未经编译,未经测试):

NSURL * url = [NSURL URLWithString:@"https://www.google.com/images/srpr/logo3w.png"];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue currentQueue]
                       completionHandler:^(NSURLResponse * resp, NSData * data, NSError * error) {

    // No error handling - check `error` if you want to
    UIImage * img = [UIImage imageWithData:data];
    [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:img waitUntilDone:YES];

}];

This avoids the long-running network request implied by the call to dataWithContentsOfURL: , so your app can continue doing things on the main thread while the data downloads for the image. 这避免了对dataWithContentsOfURL:的调用所隐含的长时间运行的网络请求,因此您的应用可以在为图像下载数据时继续在主线程上执行操作。

As a side note, you seem to be running into the same error as this question ; 附带说明一下,您似乎遇到了与此问题相同的错误; you might want to check that you're not running into object allocation problems. 您可能要检查您是否没有遇到对象分配问题。

I plugged this code into a new, iPad "Single view application" template: 我将此代码插入了新的iPad“单视图应用程序”模板中:

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

I got the image to load up properly. 我得到了正确加载的图像。 I even tried different content modes for the UIImageView, and they all worked as expected. 我什至为UIImageView尝试了不同的内容模式,它们都按预期工作。

If you start from scratch, do you get the same problem? 如果从头开始,会遇到同样的问题吗?

The error message indicates that you're sending a message of "_isResizable" to an NSData object. 错误消息表明您正在向NSData对象发送“ _isResizable”消息。 Maybe you're inadvertently setting the UIImageView 's image property equal to the NSData instead of the UIImage . 也许您无意中将UIImageViewimage属性设置为等于NSData而不是UIImage

Edit/Update 编辑/更新

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... 我的猜测是所讨论的代码没有损坏,但是附近有东西...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM