1
votes

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.)
2
Have you tried using [NSData dataWithContentsOfURL:options:error:] and seeing what error is returned?highlycaffeinated
What do you mean by "does not work"? Does any data download or is it nil?jaminguy
@highly thanks for the method. I am getting the following error. Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)iOSDevSF
According to <Foundation/FoundationErrors.h> error 256 is the NSFileReadUnknownError ("Read error (reason unknown)"). So there is not too much information in that I'm afraid...Mundi
I noticed that as well. I am digging into this post as we speak. stackoverflow.com/questions/5103788/…iOSDevSF

2 Answers

0
votes

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.

0
votes

you can try


imageView.image = img;

instead of


self.imageView.image = img;