从URL加载时应如何处理视网膜/正常图像? [英] How should retina/normal images be handled when loading from URL?
本文介绍了从URL加载时应如何处理视网膜/正常图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我了解如何以编程方式从网址为我的应用加载图片,而不是将其打包在应用内,但如何处理1x vs 2x问题?如果需要,我可以从外部源提供这两个版本,但在设置UIImage时如何处理?
I understand how to programmatically load images for my app from a URL instead of packaging them within the app but how do I handle the 1x vs 2x issue? I can serve both versions from the external source if need be but how do I handle that when setting the UIImage?
推荐答案
我'我很确定你无法以自动方式远程加载@ 2x图像文件。您将必须测试视网膜显示,然后获得适当的图像,如下所示:
I'm pretty sure you cannot load @2x image files remotely in an automated way. You will have to test for the retina display and then get the appropriate image(s), like so:
UIImage *image;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2){
// @2x
NSURL *imageURL = [NSURL URLWithString:@"http://www.example.com/images/yourImage@2x.png"];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
image = [UIImage imageWithData:imageData];
} else {
// @1x
NSURL *imageURL = [NSURL URLWithString:@"http://www.example.com/images/yourImage.png"];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
image = [UIImage imageWithData:imageData];
}
UIImageView *yourImageView = [[UIImageView alloc] initWithImage:image];
这篇关于从URL加载时应如何处理视网膜/正常图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文