@interface UIImage (Retina4)

来源:互联网 发布:qq群淘宝互刷是真的吗 编辑:程序博客网 时间:2024/06/15 14:52
#import "UIImage+Retina4.h"#import <objc/objc-runtime.h> static Method origImageNamedMethod = nil; @implementation UIImage (Retina4) + (void)initialize {    origImageNamedMethod = class_getClassMethod(self, @selector(imageNamed:));    method_exchangeImplementations(origImageNamedMethod,                                   class_getClassMethod(self, @selector(retina4ImageNamed:)));}                                                   + (UIImage *)retina4ImageNamed:(NSString *)imageName {    NSLog(@"Loading image named => %@", imageName);    NSMutableString *imageNameMutable = [imageName mutableCopy];    NSRange retinaAtSymbol = [imageName rangeOfString:@"@"];    if (retinaAtSymbol.location != NSNotFound) {        [imageNameMutable insertString:@"-568h" atIndex:retinaAtSymbol.location];    } else {        CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;        if ([UIScreen mainScreen].scale == 2.f && screenHeight == 568.0f) {            NSRange dot = [imageName rangeOfString:@"."];            if (dot.location != NSNotFound) {                [imageNameMutable insertString:@"-568h@2x" atIndex:dot.location];            } else {                [imageNameMutable appendString:@"-568h@2x"];            }        }    }    NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageNameMutable ofType:@"png"];    if (imagePath) {        return [UIImage retina4ImageNamed:imageNameMutable];    } else {        return [UIImage retina4ImageNamed:imageName];    }    return nil;} @end