简体   繁体   English

下载图像和视网膜显示

[英]Downloading images and retina display

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? 如果现在不能使用-@ 2x,如何设置要显示的正确图像?

UPDATE: 更新:

If I cache images, I need to resize images after downloading and before caching, and cache 2 images, regular and @2x? 如果我缓存图像,则需要在下载后和缓存之前调整图像大小,并缓存2张常规图像和@ 2x?

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM