How do NSBundle pathForResource:ofType: and UIImage imageWithContentsOfFile: handle scale and device modifiers?

后端 未结 1 1629
挽巷
挽巷 2021-02-13 02:46

In my iOS resource folder, I have the images:

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

I load them with:

N         


        
1条回答
  •  星月不相逢
    2021-02-13 03:21

    The explanation for imageWithContentsOfFile is in related documentation, not in the UIImage documentation itself:

    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.

    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 don't know which documentation explains the behavior of pathForResource.

    0 讨论(0)
提交回复
热议问题