0

I am having trouble getting an image to load from a URL. I have CollectionViewCell subclass with an image view setup like this:

@property (strong, nonatomic) IBOutlet UIImageView *albumCover;

I cannot get it to load images from a URL. Here is the code im using:

NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: song.albumURL]];
cell.albumCover = [[UIImageView alloc] initWithImage:[UIImage imageWithData: imageData]];

Meanwhile, this works:

cell.albumCover.image = [UIImage imageNamed:@"lock.png"];

I know the URL connection is getting the proper image back because I am monitoring it with Fiddler. Why isn't the image showing up then?

john cs
  • 2,220
  • 5
  • 32
  • 50
  • Check this link http://stackoverflow.com/questions/18506744/taking-time-to-load-the-image-from-url-to-uiimageview/18507182#18507182 It may help you – Nithinbemitk Sep 03 '13 at 03:13

3 Answers3

3

I was allocating the UIImageView object again instead of just initializing it with the image. The correct code is:

NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: song.albumURL]];
[cell.albumCover initWithImage:[UIImage imageWithData: imageData]];
john cs
  • 2,220
  • 5
  • 32
  • 50
1

I use SDWebImageView:

Asynchronous image downloader with cache support with an UIImageView category http://hackemist.com/SDWebImage/doc

Alistair A. Israel
  • 6,417
  • 1
  • 31
  • 40
0

You should use fileURLWithPath: instead of the URLWithString:.

URLWithString: is for WWW URL.

And an example:

NSString *rootPath = [[NSBundle mainBundle] resourcePath];
NSString *filePath = [rootPath stringByAppendingPathComponent:@"example.mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath isDirectory:NO];
yourMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: fileURL];
sunkehappy
  • 8,970
  • 5
  • 44
  • 65
  • thanks but i think you misunderstood what im trying to do...I am trying to load an image from the internet. – john cs Sep 03 '13 at 01:32