简体   繁体   中英

iOS background image for iphone 6

I'm trying to set a UIImage as the background of a UIView. The problem is that if I use colorWithPatternImage the background image repeats for the iPhone 6 (as far as I know it uses de @2x resolution). For the iPhone 6 plus I have no problems because it uses de @3x image but iPhone 4,4s,5,5s and 6 uses the same 2@x image and have differents screen sizes.

I'm using Xcode 6.

I have the same problem and I solved following this post

Basically you can use:

  • a single image set for 1x, 2x and R4. This covers iPhone 4, 4S and 5/5S/5C – as well as iPads scaling the app up from iPhone 4 layout
  • an “xxxxx-oversize” image set for iPhone 6
  • an “xxxxx-overize@3x” set for iPhone 6 Plus

在此输入图像描述

After than use a custom category on UIImage to load from the correct asset based on display size

#import "UIImage+ImageAdditions.h"

@implementation UIImage (ImageAdditions)

+ (NSString *)resolveAdaptiveImageName:(NSString *)nameStem {
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    if (height > 568.0f) {
        // Oversize @2x will be used for iPhone 6, @3x for iPhone 6+
        // iPads... we'll work that out later
        if (height > 667.0f) {
            return [nameStem stringByAppendingString:@"-oversize@3x"];
        } else {
            return [nameStem stringByAppendingString:@"-oversize"];
        }
    };
    return nameStem;
}

+ (UIImage *)adaptiveImageNamed:(NSString *)name {
    return [self imageNamed:[self resolveAdaptiveImageName:name]];
}

@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM