How do you set a UIImage using an absolute URL?
I am trying to set the UIImage of a tableview cell to an absolute url, here's the code i have already:
NSString * imageURL = [[stories objectAtIndex:indexPath.row] objectForKey:@"images"];
NSURL *url = [NSURL URLWithString:imageURL];
NSData *data =开发者_运维问答 [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
//set image
cell.imageView.image = img;
But for some reason this returns null as if the URL supplied is invalid. However i know this is not the case because i can make it work by typing one of the URLs as a string literal into the imageURL pointer and i know the code [[stories objectAtIndex:indexPath.row] objectForKey:@"images"] returns correct URLs as i printed the output to the log and it was fine.
Is there any reason why this would be happening that I'm clearly not seeing here?
You can use the absoluteURL
method of NSURL
and voila!
NSURL *url = [[NSURL URLWithString:imageURL] absoluteURL];
精彩评论