iOS Colorwithpattern - using a custom iPhone5 image

邮差的信 提交于 2019-12-07 03:16:50

问题


For my app's loading screen and splash screen, I used up with two different methods to display my iPhone 3, iPhone 4 and iPhone 5 images appropriately.

For the loading screen, simply adding -568h@2x to your image is enough to support iPhone5.

For the splashscreen, I used a series of (if height == ) cases to check the height of the UIScreen's bounds and sourced the appropriate image to the image view. It was apparent to me here that the -568h isn't universally recognized as an iPhone 5 image. It is only applicable to the loading screen.

Now, in my AppDelegate, I'm setting a the background image. All my subviews have a transparent background, so they are supposed to show through to the background image. However, I am having the most trouble setting up the background image here.

Simply passing in the "Background.png" Adding the -568h@2x to the end of the filename does not work. It will do the non-retina and the 3.5'' retina display, but will not pick up for the 4'' display.

ie:

self.window.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]];

If I use the above snippet of code, the iPhone4 picture is used instead of the iPhone5 picture and this is not what I want.

I moved onto trying to do the same thing as I did with the splashscreen. I had a bunch of if cases:

CGFloat height = [UIScreen mainScreen].currentMode.size.height;

if ( height == 1136 )
{
     //Choose image here
}
else if (height == 960)
{
     //Choose image here
}
else if (height == 480)
{
     //Choose image here
}
self.window.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:chosenImage]];

But the colorWithPatternImage function thinks that the chosenImage goes by 1 point = 1 pixel. So I end up with a non-retina picture attempting to fit onto the iPhone 5 screen. What exaclty does it look like? Looks like only the top left quadrant of the image I wanted is displayed over the entire iPhone 5 screen. It looks like no retina scaling was applied.

I need the iPhone to recognize that I have a retina-friendly iPhone 5 picture to be used as the background. How do I do this?


回答1:


I found this code here, perhaps it helps you:

UIImage *tmpImage = [UIImage imageNamed:@"background-568h@2x.png"];
UIImage *backgroundImage = [UIImage imageWithCGImage:[tmpImage CGImage]
                                               scale:2.0
                                         orientation:UIImageOrientationUp];
self.window.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];


来源:https://stackoverflow.com/questions/12847541/ios-colorwithpattern-using-a-custom-iphone5-image

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