Xcode asset catalog support for 3.5“, 4”, and 4.7"

2019-07-15 10:12发布

Xcode offers asset buckets for 1x, 2x, 4" 2x, and 3x assets. My fullscreen assets are all wonky with this configuration, because the 2x bucket is used for 3.5" and 4.7" screens. Currently I have a UIImage category that introspects the current screen size, and selects a "*-3.5" asset if the device seems to be 3.5".

This is clunky and not cool. Seeing that Xcode caters to all the different device sizes for their LaunchImage asset, I was hoping there was some way to supply device specific assets for non launch image assets without resorting to code.

3条回答
Ridiculous、
2楼-- · 2019-07-15 10:23

I have reported a bug to Apple about this since mid November 2014 and I just noticed they marked it as No Value... which gives me the impression Apple has omitted an additional slot for iPhone 6 on purpose. My guess is they now want the 2x slot to be used for iPhone 6, and maybe they're reducing support to iPhone 4s as it's getting old.

If you really want to keep supporting the iPhone 4s, I'd suggest to use iPhone 6 sized images in the 2x slot, and then use the following method to load your images:

+(UIImage *)loadImageNamed:(NSString *)imageName
{
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGFloat screenHeight = MAX(screenSize.width, screenSize.height);
    CGFloat const IPHONE_4_SCREEN_HEIGHT = 480;
    UIImage *image = [UIImage imageNamed:imageName];
    if(screenHeight == IPHONE_4_SCREEN_HEIGHT) {
        CGFloat const xScale = .85333333;//x conversion ratio from iPhone 6's 375 pts width screen to iPhone 4's 320 pts width screen
        CGFloat const yScale = .71964018;//y conversion ratio from iPhone 6's 667 pts height screen to iPhone 4's 480 pts height screen
        CGSize newSize = CGSizeMake(xScale * image.size.width, yScale * image.size.height);
        UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);

        [image drawInRect:(CGRect){0, 0, newSize}];
        UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return resizedImage;
    }
    return image;
}

Resizing a big image (iPhone 6) to a smaller one (iPhone 4) doesn't lose much quality even if the aspect ration is not the same.

查看更多
老娘就宠你
3楼-- · 2019-07-15 10:31

Using an existing project when moving to Xcode 6 and iOS 8 creates some initial issues. I solved this by changing the image set from Device specific to Universal. This removes the R4 image option and leaves you with @1x, @2xand @3x. If you use for example a background image that needs to fit the screen then I will recommend that you find an image that does not have to fit 100 % perfect to look good, and set image to be displayed as Aspect Fill.

查看更多
倾城 Initia
4楼-- · 2019-07-15 10:39

I have the same problem and got confirmation from apple that I have to wrote code for that. For example you can try like this Fullscreen images on iPhone 6 with Asset Catalogs

But I actually went lazy about it by using one 2x for 4" & 4.7" screen and another 2x for 3.5" screen

查看更多
登录 后发表回答