I'm trying to download an image from the NSUrl imageURL and display it in a UIImageView with retina pixel density by adding the @2x; this is to be displayed on an iPhone 4 or later. I've used some code from here: How do I download and save a file locally on iOS using objective C?
It shows nothing on the imageView when I start it, and the last NSLog line prints out some really tiny dimensions for the image, suggesting that it didn't really download one. What is wrong with my code besides it having really bad error checking?
- (void) presentImage: (NSURL*) imageURL{
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
NSLog([imageURL description]);
if ( imageData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"0@2x.png"];
[imageData writeToFile:filePath atomically:YES];
}
else NSLog(@"Error when loading image!!!");
imageData = [NSData dataWithContentsOfFile:@"0@2x.png"];
UIImage *image = [[UIImage alloc] initWithData: imageData];
NSLog(@"%f1 x %f2", image.size.width, image.size.height);
[self.imageView setImage: image];
}
EDIT: OK, I fixed that. I feel stupid. I was supposed to put in
imageData = [NSData dataWithContentsOfFile:@filePath];
and put filePath in scope nstead of just putting in @"0@2x.png".