UIImage from URL difficulty

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://img.ioscake.com/iphone/car-530.jpg"];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *img = [[UIImage alloc] initWithData:data] ;
self.imageView.image = img;

I have tried this

  • Xcode “Use of undeclared identifier”
  • Which persistent store is used by default in core data in iPhone
  • How to change iPhone UITableView delete button title while editing it
  • How to turn on/off airplane mode in IOS 5.1 using private API
  • iPhone/iPod Touch: application executable contains unsupported architecture(s): armv7s (-19031)
  • Application Loader Error: XML Parsing Error has occurred
  • NSURL *imageURL = [NSURL URLWithString:@"http://img.ioscake.com/iphone/3m23pb3l85T65R05S4b8462d2236c156a1ce3.jpg"];
    NSData *data = [NSData dataWithContentsOfURL:imageURL];
    UIImage *img = [[UIImage alloc] initWithData:data] ;
    self.imageView.image = img;
    

    and even this

    NSString * urlString = [@"http://img.ioscake.com/iphone/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 Solutions Collect From Internet About “UIImage from URL difficulty”

    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.

    you can try


    imageView.image = img;

    instead of


    self.imageView.image = img;