问题
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