How to specify size for iPhone 6/7 customised edge-to-edge image?

后端 未结 12 950
死守一世寂寞
死守一世寂寞 2020-11-28 01:59

Say I want a bundled image to take up all available screen width in an iPhone app - for example a banner. I\'d create my_banner.png with width 320px

相关标签:
12条回答
  • 2020-11-28 02:06

    I raised the same question to apple technical support and they confirm that for fullscreen image it can't be done in asset catalog: "Currently, there is no way for the Asset Catalog to load device specific images. If your app needs to support device specific images you will need to implement your own code to detect the screen size and choose the appropriate image. You can file an enhancement request by using the following link. Be sure to explain your use case for this feature. "

    0 讨论(0)
  • 2020-11-28 02:06

    I checked the naming convention of a launch image generated from an asset catalog via Xcode 6 and the landscape version for iPhone 6+, for example, had: LaunchImage-Landscape-736h@3x.png

    Based on that, I'd presume it would be as follows, for retina devices, assuming a base file desert.png:

    • desert@2x : iPhone 4s (320 x 420)
    • desert-568h@2x : iPhones 5, 5C and 5S (320 x 568)
    • desert-667h@2x : iPhone 6 (375 x 667)
    • desert-736h@3x : iPhone 6+ (414 x 736)
    • desert@2x~ipad : iPad (1024 x 768)
    0 讨论(0)
  • 2020-11-28 02:07

    I am using the following trick as some stuff actually works:

    • Asset Catalog for specific devices
    • Specify images for 1x, 2x on the base of 320x640
    • Specify images for 4 2x and 3x on the base of 320x568 (iPhone 5)
    • Create a new Images set for the iPhone 6 specifically (as this is the only device that makes trouble with edge to edge bindings)
    • Only provide 2x image for iPhone 6 in full resolution (750x1334)

    Declare a constant

    #define IS_IPHONE_6 [[UIScreen mainScreen]nativeBounds].size.width == 750.0 ? true : false
    

    and use it like this:

    UIImage *image = [UIImage imageNamed:@"Default_Image_Name"];
    if(IS_IPHONE_^) {
        image = [UIImage imageNamed:@"Iphone6_Image_Name"];
    }
    

    this might be not the most beautiful solution, but it works, at least as long as apple does not provide a better API for edge to edge bindings.

    0 讨论(0)
  • 2020-11-28 02:07

    Please try this class to change the image name programmatically.

    import UIKit
    
    class AGTools: NSObject {
        class func fullWidthImage(imageName: String!) -> String!{
            let screenWidth = UIScreen.mainScreen().bounds.size.width
            switch (screenWidth){
            case 320:
                // scale 2x or 1x
                return (UIScreen.mainScreen().scale > 1.0) ? "\(imageName)@2x" : imageName
            case 375:
                return "\(imageName)-375w@2x"
            case 414:
                return "\(imageName)-414w@3x"
            default:
                return imageName
            }
        }
    }
    

    use this method like this.

    _imgTest.image = UIImage(named: AGTools.fullWidthImage("imageName"))
    
    0 讨论(0)
  • 2020-11-28 02:09

    FIRST of all, you need to configure your imageView to cover all the screen, Autolayout will help a lot for this job, take a look on the link below and find how to Pin the constraints (Leading Space, Trailing Space, Top Space and Bottom Space) using Storyboards:

    http://www.thinkandbuild.it/learn-to-love-auto-layout/

    enter image description here

    SECOND step is create device specific image sets on your image assets (image below), to display different images according to device.

    enter image description here

    Check out this infographic: http://www.paintcodeapp.com/news/ultimate-guide-to-iphone-resolutions

    It explains the differences between old iPhones, iPhone 6 and iPhone 6 Plus. You can see comparison of screen sizes in points, rendered pixels and physical pixels

    That's all

    enter image description here

    enter image description here

    Please, give a feedback if you have any trouble.

    0 讨论(0)
  • 2020-11-28 02:11

    It seems to me that a lot of these answers want to address how to constrain the imageView, where I think you are concerned with loading the correct media file? I would come up with my own future extensible solution, something like this:

    "UIImage+DeviceSpecificMedia.h" - (a category on UIImage)

    Interface:

    #import <UIKit/UIKit.h>
    
    typedef NS_ENUM(NSInteger, thisDeviceClass) {
    
        thisDeviceClass_iPhone,
        thisDeviceClass_iPhoneRetina,
        thisDeviceClass_iPhone5,
        thisDeviceClass_iPhone6,
        thisDeviceClass_iPhone6plus,
    
        // we can add new devices when we become aware of them
    
        thisDeviceClass_iPad,
        thisDeviceClass_iPadRetina,
    
    
        thisDeviceClass_unknown
    };
    
    thisDeviceClass currentDeviceClass();
    
    @interface UIImage (DeviceSpecificMedia)
    
    + (instancetype )imageForDeviceWithName:(NSString *)fileName;
    
    @end
    

    Implementation:

    #import "UIImage+DeviceSpecificMedia.h"
    
    thisDeviceClass currentDeviceClass() {
    
        CGFloat greaterPixelDimension = (CGFloat) fmaxf(((float)[[UIScreen mainScreen]bounds].size.height),
                                                        ((float)[[UIScreen mainScreen]bounds].size.width));
    
        switch ((NSInteger)greaterPixelDimension) {
            case 480:
                return (( [[UIScreen mainScreen]scale] > 1.0) ? thisDeviceClass_iPhoneRetina : thisDeviceClass_iPhone );
                break;
            case 568:
                return thisDeviceClass_iPhone5;
                break;
            case 667:
                return thisDeviceClass_iPhone6;
                break;
            case 736:
                return thisDeviceClass_iPhone6plus;
                break;
            case 1024:
                return (( [[UIScreen mainScreen]scale] > 1.0) ? thisDeviceClass_iPadRetina : thisDeviceClass_iPad );
                break;
            default:
                return thisDeviceClass_unknown;
                break;
        }
    }
    
    @implementation UIImage (deviceSpecificMedia)
    
    + (NSString *)magicSuffixForDevice
    {
        switch (currentDeviceClass()) {
            case thisDeviceClass_iPhone:
                return @"";
                break;
            case thisDeviceClass_iPhoneRetina:
                return @"@2x";
                break;
            case thisDeviceClass_iPhone5:
                return @"-568h@2x";
                break;
            case thisDeviceClass_iPhone6:
                return @"-667h@2x"; //or some other arbitrary string..
                break;
            case thisDeviceClass_iPhone6plus:
                return @"-736h@3x";
                break;
    
            case thisDeviceClass_iPad:
                return @"~ipad";
                break;
            case thisDeviceClass_iPadRetina:
                return @"~ipad@2x";
                break;
    
            case thisDeviceClass_unknown:
            default:
                return @"";
                break;
        }
    }
    
    + (instancetype )imageForDeviceWithName:(NSString *)fileName
    {
        UIImage *result = nil;
        NSString *nameWithSuffix = [fileName stringByAppendingString:[UIImage magicSuffixForDevice]];
    
        result = [UIImage imageNamed:nameWithSuffix];
        if (!result) {
            result = [UIImage imageNamed:fileName];
        }
        return result;
    }
    
    @end
    
    0 讨论(0)
提交回复
热议问题