Download images from a web server to display iOS retina

I upload images from a web server to display as a table in an iOS application using the following code:

NSURL *url = [NSURL URLWithString:[imageArray objectAtIndex:indexPath.row]];
UIImage *myImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
cell.imageView.image = myImage;

The image is a 60x60 and 120x120 placeholder for displaying the retina. I'm going to assume that the user has an iPhone 4. However, if the image size of 120x120 does not fix the problem, it just gets too large for the image. If I have a 60x60 image size on a web server that is, then the image fits fine, but its a little fuzzy. Does anyone know how to fix this problem?

Thank!

+3
source share
4 answers

, UIImageView 60x60 , 60x60 120x120 .

a UIImageView 60x60 60x60 1.0 120x120 2.0 . , UIImage size 60x60 , scale .

, ( ), :

if ([UIScreen mainScreen].scale == 1.0) {
    // Build URL for 60x60 pixels image
}
else {
    // Build URL for 120x120 pixels image   
}

UIImage 60x60 scale:

NSData *imageData = [NSData dataWithContentsOfURL:url];
CFDataRef cfdata = CFDataCreate(NULL, [imageData bytes], [imageData length]);
CGDataProviderRef imageDataProvider = CGDataProviderCreateWithCFData (cfdata);
CGImageRef imageRef = CGImageCreateWithJPEGDataProvider(imageDataProvider, NULL, true, kCGRenderingIntentDefault);
UIImage *image = [[UIImage alloc] initWithCGImage:imageRef 
                                            scale:[UIScreen mainScreen].scale 
                                      orientation:UIImageOrientationUp];
CFRelease (imageRef);
CFRelease (imageDataProvider);
CFRelease(cfdata);

, .

+4

, , Retina. , ( "@2x" ).

/ UIView : setScale: , , , Retina Display.

0
source

Well, the answer is as simple as it can be:

I would recommend always downloading double-density images from the server (since there are very few users with non-retina displays) and settings in the image view.

What you did wrong is that you did not set that imageView automatically matches the content. You can do this either in IB by selecting ImageView and setting the contentMode (mode) parameter to scale for Fill or by code:

imageView.contentMode = UIViewContentModeScaleToFill;
0
source

All Articles