且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

NSBundle pathForResource:ofType:和UIImage imageWithContentsOfFile:处理比例和设备修饰符?

更新时间:2023-02-14 07:59:43

的解释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.