UIImage imageNamed: does not automatically pick retina @2x images

徘徊边缘 提交于 2019-12-03 09:16:19

问题


Let's say I have three images in a bundle or asset catalog:

  1. Default~iphone.png
  2. Default@2x~iphone.png
  3. Default-568h@2x.png

On iOS 4 and later, the UIImage constructor can take the image name as follows:

[UIImage imageNamed:@"Default"];

When I am on a 3.5 inch retina display (iphone) it automatically picks image (2). If on a non-retina display it picks (1). This is great.

I named image 3 as specified for the 4 inch retina (iPhone 5) launch image. Is there a way to name image (3), so that when I am running on a 4 inch retina display, it is returned with the same UIImage constructor?

Perhaps this is not implemented yet, or I expect too much from the convenience... I am just trying to avoid any conditional logic in my code to pick the image based on the screen dimensions.


回答1:


I also had the same issue and it turned out that there is no such behavior for the iPhone 5/iPod Touch 5th Generation.

You have to manually determine if your App is running on such a device and change the filename accordingly.

I've used this method to check if my App is running on an iPhone 5/iPod Touch 5th Gen.:

#define IS_PHONEPOD5() ([UIScreen mainScreen].bounds.size.height == 568.0f && [UIScreen mainScreen].scale == 2.f && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

Then you can adjust the image name like this:

if(IS_PHONEPOD5()) {
   myImageView.image = [UIImage imageNamed:@"MyImage-568h.png"];
} else {
   myImageView.image = [UIImage imageNamed:@"MyImage.png"];
}

Update
I also found a UIImage category on github (Link) that implements what you're looking for. It does not have a fallback for non existing files, but you could implement it easily by yourself.




回答2:


There is no 4" image type. The only thing that is different from everything else is the inclusion of Default-568h@2x.png which gets used as the launch image for an iPhone 5 and signals the OS that your application supports the longer screen and shouldn't be letterboxed.

You have to deal in code or with autolayouts with the different screen sizes. There's no special, automatic image type. It's either a standard screen image type or a retina image type, the same as it's been since iOS 4.




回答3:


New API in iOS 8 allows for you to load retina versions of a named image, previously you had to determine the file names yourself:

UIImage* retinaImage = [UIImage imageNamed:@"ImageName" 
    inBundle:nil compatibleWithTraitCollection:nil];

NSLog(@"screen scale: %2.2f retinaImage: %@",
    [UIScreen mainScreen].nativeScale, retinaImage);


来源:https://stackoverflow.com/questions/12733155/uiimage-imagenamed-does-not-automatically-pick-retina-2x-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!