Question

If I download some images from server should I resize it to support retina devises?

And if now I can not use -@2x, how can I set correct image to display?

UPDATE:

If I cache images, I need to resize images after downloading and before caching, and cache 2 images, regular and @2x?

Was it helpful?

Solution

your app should retrieved those image in retina size. to get the non-retina size, you can scale it manually then save it. here the example code :

UIImage *image = [UIImage imageNamed:@"yourRetinaImage@2x.png"];

// set non-retina size from current image
CGSize size = CGSizeMake(image.size.width / 2., image.size.height / 2.);


/** scale the image */

UIGraphicsBeginImageContext(size);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0.0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), image.CGImage);

UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


/** save scaled image */

NSString *basePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
// save with same name but without suffix "@2x"
NSString *filePath = [NSString stringWithFormat:@"%@/%@", basePath, @"yourRetinaImage"];

@try {
    [UIImagePNGRepresentation(scaledImage) writeToFile:filePath options:NSAtomicWrite error:nil];
} @catch (NSException *exception) {
    NSLog(@"error while saving non-retina image with exception %@", exception);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top