flashlib
4/24/2015 - 8:21 AM

UIImage+Autoresize.h

//
//  UIImage+Autoresize.m
//  UIImage+Autoresize
//
//  Created by kevin delord on 24/04/14.
//  Copyright (c) 2014 Kevin Delord. All rights reserved.
//

#import "UIImage+Autoresize.h"
#import <objc/runtime.h>

@implementation UIImage (Autoresize)

#pragma mark - UIImage Initializer

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method origImageNamedMethod = class_getClassMethod(self.class, @selector(imageNamed:));
        method_exchangeImplementations(origImageNamedMethod, class_getClassMethod(self.class, @selector(dynamicImageNamed:)));
    });
}

+ (UIImage *)dynamicImageNamed:(NSString *)imageName {

    // only change the name if no '@2x' or '@3x' are specified
    if ([imageName rangeOfString:@"@"].location == NSNotFound) {

        CGFloat h = [UIScreen mainScreen].bounds.size.height;
        CGFloat w = [UIScreen mainScreen].bounds.size.width;
        CGFloat scale = [UIScreen mainScreen].scale;

        // generate the current valid file extension depending on the current device screen size.
        NSString *extension = @"";
        if (scale == 3.f) {
            extension = @"@3x";
        } else if (scale == 2.f && h == 568.0f && w == 320.0f) {
            extension = @"-568h@2x";
        } else if (scale == 2.f && h == 667.0f && w == 375.0f) {
            extension = @"-667h@2x";
        } else if (scale == 2.f && h == 480.0f && w == 320.0f) {
            extension = @"@2x";
        }

        // add the extension to the image name
        NSRange dot = [imageName rangeOfString:@"."];
        NSMutableString *imageNameMutable = [imageName mutableCopy];
        if (dot.location != NSNotFound)
            [imageNameMutable insertString:extension atIndex:dot.location];
        else
            [imageNameMutable appendString:extension];

        // if exist returns the corresponding UIImage
        if ([[NSBundle mainBundle] pathForResource:imageNameMutable ofType:@""]) {
            return [UIImage dynamicImageNamed:imageNameMutable];
        }
    }
    // otherwise returns an UIImage with the original filename.
    return [UIImage dynamicImageNamed:imageName];
}

@end
//
//  UIImage+Autoresize.h
//  UIImage+Autoresize
//
//  Created by kevin delord on 24/04/14.
//  Copyright (c) 2014 Kevin Delord. All rights reserved.
//

#ifndef UIImage_Autoresize_h__
#define UIImage_Autoresize_h__

#import <UIKit/UIKit.h>

/**
 * A categorized class of UIImage to add a naming convetion to deal with different image files:
 *
 * **none** if @1x small old phones
 * **@2x** for iPhone 4
 * **-568h@2x** for iPhone 5
 * **-667h@2x** for iPhone 6
 * **@3x** for iPhone 6 Plus
 */
@interface UIImage (Autoresize)

#pragma mark - UIImage Initializer

/**
 * Method to override the UIImage::imageNamed: method with the retina4ImageNamed: one.
 * The new method and its implementation will be executed instead of the default UIImage::imageNamed:
 * The user don't need to do anything.
 */
+ (void)load;

/**
 * Returns a new UIImage object created from a filename.
 *
 * @discussion If needed, this method will automatically add the needed image suffix for the current device:
 * - "@2x"
 * - "-568h@2x"
 * - "-667h@2x"
 * - "@3x"
 * Important: the given filename should NOT contain any size-extension, only a name and its file type.
 *
 * @param imageName The NSString object representing the filename of the image.
 * @return An UIImage created from a given string.
 */
+ (UIImage *)dynamicImageNamed:(NSString *)imageName;

@end

#endif