Retina显示来自URL的图像

Ted*_*Ted 6 xcode image objective-c ios retina-display

我有一些我需要从网上获取的图片.只使用来自URL的数据.
它们需要在Retina Display上正确显示.
当我从网络上获取图像时,它们仍然看起来像素化.我需要将图像的比例设置为视网膜显示(2.0),但我必须遗漏一些东西.这是我到目前为止所做的.

UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:@"http://www.msdomains.com/tmp/test.png"];

CGRect labelFrame = CGRectMake(0,0,64,64);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:labelFrame];
imageView.contentScaleFactor = [UIScreen mainScreen].scale;

[imageView setImage:img];
[self addSubview:imageView];
[imageView release];
Run Code Online (Sandbox Code Playgroud)

pre*_*pre 7

尝试#@2x.png在网址末尾添加.这不会改变URL,但图像将被识别为视网膜@ 2x图像.它对我有用,但我在SDWebImage中使用了这个方法.

例如使用http://www.msdomains.com/tmp/test.png#@2x.png.


Mar*_*nny 3

您的代码应该几乎可以按原样工作。我不知道你的图像的原始尺寸是多少,但我猜它们是 64x64 像素。为了正确缩小,原始图像需要为 128x128 像素。

作为测试,以下代码在模拟器和我的 iPhone 4 上以 Retina 分辨率正确显示了我的照片:

UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.seenobjects.org/images/mediumlarge/2006-08-19-native-lilac.jpg"]]];

CGRect labelFrame = CGRectMake(0, 0, 375, 249.5);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:labelFrame];

[imageView setImage:img];
[self.view addSubview:imageView];
Run Code Online (Sandbox Code Playgroud)

请注意,尺寸UIImageView为 375x249.5 点,这是照片原始(像素)尺寸的一半。另外,设置contentScaleFactor似乎没有必要。

(顺便说一句,在这种情况下,我看不出@2x在 URL 上指定会有帮助,因为调用dataWithContentsOfURL:将返回一个不透明的数据块,而没有留下任何文件名的痕迹。正是这些不透明的数据然后被传递给imageWithData:加载图像。)