简体   繁体   English

如何在视图中正确地将图像(不是资源)缩放到 iPhone Retina 显示?

[英]How to scale images (not resources) to iPhone Retina display correctly in a view?

I have a UIImage, from either device's camera or downloaded from a URL.我有一个 UIImage,来自任一设备的相机或从 URL 下载。 Say resolution is 800x600.说分辨率是800x600。

I want to display a scaled image in a UIImageView which is 200x150, and I have a proper image scaling code.我想在 200x150 的 UIImageView 中显示缩放图像,并且我有正确的图像缩放代码。

So, I just scale the UIImage to the UIImageView's frame.size (again, 200x150).所以,我只是将 UIImage 缩放到 UIImageView 的frame.size (同样,200x150)。 On a non-retina display it looks OK, but on a retina display it looks bad, like a smaller picture was scaled up, because the actual frame in pixels is 400x300.在非视网膜显示器上它看起来不错,但在视网膜显示器上它看起来很糟糕,就像一张较小的图片被放大了,因为以像素为单位的实际帧是 400x300。

The question is - should I, manually multiply the frame.size.width / height by 2 when it's a retina display when I resize, so it'll be resized to 400x300, or is there a built in feature for that?问题是 - 我是否应该在调整大小时手动将frame.size.width / height乘以 2,以便将其调整为 400x300,或者是否有内置功能?

Since the UIImage is not a resource on the device, I don't have the @2x option.由于 UIImage 不是设备上的资源,因此我没有@2x选项。

Keep the frame size of the image view at 200x150 in all cases.在所有情况下,保持图像视图的帧大小为 200x150。 In case of non-retina display scale the image to 200x150.在非视网膜显示器的情况下,将图像缩放到 200x150。 On retina display devices scale it to 400x300.在视网膜显示设备上将其缩放到 400x300。 Set the contentMode of the UIImageView to UIViewContentModeScaleAspectFit .UIImageViewcontentMode设置为UIViewContentModeScaleAspectFit

When you get the image in say 800x600, can't you make a UIImage that is scaled to 400x300 and save it temporarily with the @2x extension, maybe to the Documents folder or tmp folder, and then save another UIImage scaled to 200x150 and save it without the @2x in the same directory.当你得到 800x600 的图像时,你不能制作一个缩放到 400x300 的 UIImage 并使用@2x 扩展名临时保存它,可能到 Documents 文件夹或 tmp 文件夹,然后保存另一个 UIImage 并保存缩放到 2它在同一目录中没有@2x。

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

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