NSBundle pathForResource:ofType:和UIImage imageWithContentsOfFile:处理比例和设备修饰符? [英] How do NSBundle pathForResource:ofType: and UIImage imageWithContentsOfFile: handle scale and device modifiers?

查看:155
本文介绍了NSBundle pathForResource:ofType:和UIImage imageWithContentsOfFile:处理比例和设备修饰符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iOS资源文件夹中,我有图片:

In my iOS resource folder, I have the images:

foo~iphone.png
foo@2x~iphone.png
foo~ipad.png
foo@2x~ipad.png

我加载它们:

NSString *fileName = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:fileName];

实验性地:

在iPhone上, filename /path/to/bundle/foo~iphone.png ,在视网膜iPhone上,它加载了 @ 2x 版本。

On the iPhone, filename is /path/to/bundle/foo~iphone.png, and on the retina iPhone, it loads the @2x version.

在iPad上,文件名 /path/to/bundle/foo~ipad.png ,在视网膜iPad上,它加载 @ 2x 版本。

On the iPad, filename is /path/to/bundle/foo~ipad.png, and on the retina iPad, it loads the @2x version.

也就是说,它加载我希望的图像,但 pathForResource 处理设备修改器,和 imageWithContentsOfFile 处理缩放修改器。

That is, it loads the image I'd hope, but pathForResource handles the device modifier, and imageWithContentsOfFile handles the scale modifier.

我想知道原因。 文档 of pathForResource 没有提及设备修饰符,文档 imageWithContentsOfFile 没有提及缩放修饰符。

I'd like to know why. The documentation of pathForResource says nothing about device modifiers, and the documentation of imageWithContentsOfFile says nothing about scale modifiers.

是否有单独的文档准确显示每个函数的作用?

Is there separate documentation that indicates precisely what each function does?

推荐答案

的解释imageWithContentsOfFile 位于相关文档,不在 UIImage 文档中:


在具有高分辨率屏幕的设备上,imageNamed:,imageWithContentsOfFile:和initWithContentsOfFile:方法会自动查找所请求图像的版本,其名称中包含@ 2x修饰符。如果找到一个,则会加载该图像。如果您未提供给定图像的高分辨率版本,则图像对象仍会加载标准分辨率图像(如果存在)并在绘图期间对其进行缩放。

On devices with high-resolution screens, the imageNamed:, imageWithContentsOfFile:, and initWithContentsOfFile: methods automatically looks for a version of the requested image with the @2x modifier in its name. If it finds one, it loads that image instead. If you do not provide a high-resolution version of a given image, the image object still loads a standard-resolution image (if one exists) and scales it during drawing.

加载图像时,UIImage对象会根据图像文件的后缀自动将大小和比例属性设置为适当的值。对于标准分辨率图像,它将scale属性设置为1.0,并将图像的大小设置为图像的像素尺寸。对于文件名中带有@ 2x后缀的图像,它将scale属性设置为2.0,并将width和height值减半以补偿比例因子。这些减半的值与您需要在逻辑坐标空间中使用的基于点的尺寸正确关联以呈现图像。

When it loads an image, a UIImage object automatically sets the size and scale properties to appropriate values based on the suffix of the image file. For standard resolution images, it sets the scale property to 1.0 and sets the size of the image to the image’s pixel dimensions. For images with the @2x suffix in the filename, it sets the scale property to 2.0 and halves the width and height values to compensate for the scale factor. These halved values correlate correctly to the point-based dimensions you need to use in the logical coordinate space to render the image."

I不知道哪个文档解释了 pathForResource 的行为。

I don't know which documentation explains the behavior of pathForResource.

这篇关于NSBundle pathForResource:ofType:和UIImage imageWithContentsOfFile:处理比例和设备修饰符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆