UIImage from URL difficulty

梦想与她 提交于 2019-12-24 11:35:45

问题


The following code works to load a UIImage from a URL. However, when I try to load an image from Craigslist, the image does not work. I have verified the craigslist imagePath is working.

NSURL *imageURL = [NSURL URLWithString:@"http://images.free-extras.com/pics/c/car-530.jpg"];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *img = [[UIImage alloc] initWithData:data] ;
self.imageView.image = img;

I have tried this

NSURL *imageURL = [NSURL URLWithString:@"http://images.craigslist.org/3m23pb3l85T65R05S4b8462d2236c156a1ce3.jpg"];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *img = [[UIImage alloc] initWithData:data] ;
self.imageView.image = img;

and even this

NSString * urlString = [@"http://images.craigslist.org/3m23pb3l85T65R05S4b8462d2236c156a1ce3.jpg" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL * imageURL = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *img = [[UIImage alloc] initWithData:data] ;
self.imageView.image = img;

Any help is much appreciated. My Error message reads:

Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)

回答1:


have you checked if the data and the probably evolving image structure are not nil? Normally this should not happen, but it might be valuable to point out what went wrong.

The loading code looks good to me - I use something similar.




回答2:


you can try


imageView.image = img;

instead of


self.imageView.image = img;



来源:https://stackoverflow.com/questions/6949010/uiimage-from-url-difficulty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!