Skip to content

Instantly share code, notes, and snippets.

@bromanko
Forked from bstahlhood/UIImage+Retina4.h
Created September 26, 2012 14:55
Show Gist options
  • Save bromanko/3788510 to your computer and use it in GitHub Desktop.
Save bromanko/3788510 to your computer and use it in GitHub Desktop.
Swizzled UIImage imageNamed for iPhone 5
//
// UIImage+Retina4.h
// StunOMatic
//
// Created by Benjamin Stahlhood on 9/12/12.
// Copyright (c) 2012 DS Media Labs. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIImage (Retina4)
@end
//
// UIImage+Retina4.m
// StunOMatic
//
// Created by Benjamin Stahlhood on 9/12/12.
// Copyright (c) 2012 DS Media Labs. All rights reserved.
//
#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
//
// UIImage+FullScreen+FullScreen.h
// StunOMatic
//
// Created by Benjamin Stahlhood on 9/12/12.
// Modified by bromanko on 9/26/12
// Copyright (c) 2012 DS Media Labs. All rights reserved.
//
@interface UIImage (FullScreen)
@end
//
// UIImage+FullScreen+FullScreen.m
// StunOMatic
//
// Created by Benjamin Stahlhood on 9/12/12.
// Modified by bromanko on 9/25/12
// Copyright (c) 2012 DS Media Labs. All rights reserved.
//
#import <objc/runtime.h>
static Method origImageNamedMethod = nil;
@implementation UIImage (FullScreen)
+ (void)load {
if ([UIScreen mainScreen].scale == 2.f && [UIScreen mainScreen].bounds.size.height == 568.0f) {
origImageNamedMethod = class_getClassMethod(self, @selector(imageNamed:));
method_exchangeImplementations(origImageNamedMethod, class_getClassMethod(self, @selector(fullScreenImageNamed:)));
}
}
+ (UIImage *)fullScreenImageNamed:(NSString *)imageName {
NSMutableString *imageNameMutable = [imageName mutableCopy];
NSRange retinaAtSymbol = [imageName rangeOfString:@"@"];
if (retinaAtSymbol.location != NSNotFound) {
[imageNameMutable insertString:@"-568h" atIndex:retinaAtSymbol.location];
} else {
NSRange dot = [imageName rangeOfString:@"." options:NSBackwardsSearch];
if (dot.location != NSNotFound) {
[imageNameMutable insertString:@"-568h@2x" atIndex:dot.location];
} else {
[imageNameMutable appendString:@"-568h@2x"];
}
}
// remove extension
NSRange dot = [imageNameMutable rangeOfString:@"." options:NSBackwardsSearch];
NSString *extension = @"png";
if (dot.location != NSNotFound) {
NSRange range;
range.location = dot.location;
range.length = imageNameMutable.length - range.location;
NSRange extensionRange = range;
extensionRange.location += 1;
extensionRange.length -= 1;
extension = [imageNameMutable substringWithRange:extensionRange];
[imageNameMutable deleteCharactersInRange:range];
}
// get path
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageNameMutable ofType:extension];
NSString *imageNameWithout2x = [imageNameMutable stringByReplacingOccurrencesOfString:@"@2x" withString:@""];
if (imagePath) {
return [UIImage fullScreenImageNamed:imageNameWithout2x];
} else {
return [UIImage fullScreenImageNamed:imageName];
}
}
@end
@bromanko
Copy link
Author

This fork fixes the issue where images are rendered at 2x resolution. The problem is that the original would call [UIImage imageNamed:] with a filename that includes "@2x".

@waynehartman noticed this issue and commented that you can create a new image and set the scale manually but I preferred to correct this without generating a new image.

@bstahlhood
Copy link

Awesome. Great fix. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment