UITabBarItem image size in retina display
I have a TabBarController application that gets all the images from the web, including the icons for the tabBarController. What I want is that images look good when the device has retina display.
This is what I am doing:
- Resizing down images depending开发者_运维知识库 on the screen scale.
- Setting the view content scale: imageView.contentsScale = [UIScreen mainScreen].scale;
It is working fine for standard images with UIImageView, however I can not figure out how to do this for UITabBarItems, since I have no access to either the frame or the contentScale.
Any ideas?
Thanks!
When creating the UIImage, you can set the scale of the image to 2.0 for retina size image resources. Here is a sample of how I did it:
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
UIImage *image = [UIImage imageWithData:data];
CGImageRef cgimage = image.CGImage;
image = [UIImage imageWithCGImage:cgimage scale:2.0 orientation:UIImageOrientationUp];
Now you can use this image on your UITabBarItem.
You can create two versions of the image and name them image.png for the 30px and image@2x.png for the 60px. Then use this:
UIImage *image = [UIImage imageNamed:@"image.png"];
The right resolution image will be loaded depending on the display on the device.
what about determining whether the device is retina or not, and download different assets accordingly?
精彩评论