Skip to content

Instantly share code, notes, and snippets.

@basecode
Last active October 7, 2015 09:11
Show Gist options
  • Save basecode/3139509 to your computer and use it in GitHub Desktop.
Save basecode/3139509 to your computer and use it in GitHub Desktop.
UIImage+Additions
// UIImage+Additions.h
// Copyright (c) 2012 Tobias Reiss (MIT License)
#import <UIKit/UIKit.h>
@interface UIImage (Additions)
+ (UIImage *)deviceAgnosticImageNamed:(NSString *)name;
- (UIImage *)deviceAgnosticImageFromRect:(CGRect)frame;
@end
// UIImage+Additions.h
// Copyright (c) 2012 Tobias Reiss (MIT License)
#import "UIImage+Additions.h"
@implementation UIImage (Additions)
/*
* deviceAgnosticImageNamed
*
* This method tackles a common problem where
* devs want to use iPhone's "@2x" image on iPad's "@1x" devices
* and a "@4x" version on iPad's "@2x" devices.
* "@4x" means double the size of the iPhone's @2x size.
*
* Example:
* This call `[UIImage deviceAgnosticImageNamed:@"myImage.png"];`
* would return the following images.
* - iPhone (1x) -> "myImage.png"
* - iPhone (2x) -> "myImage@2x.png"
* - iPad (1x) -> "myImage@2x.png"
* - ipad (2x) -> "myImage@4x.png"
*
* That means your bundle should contain the following images:
* - myImage.png
* - myImage@2x.png
* - myImage@4x.png
*
*/
+ (UIImage *)deviceAgnosticImageNamed:(NSString *)name
{
// determine iPad
BOOL isIpad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
if (isIpad) {
NSString *ext = ext = name.pathExtension;
// determine resolution
NSString *imageName = [NSString stringWithFormat:@"%@@%dx",
[name substringToIndex:MAX(0, name.length - ext.length - 1)],
((int)[[UIScreen mainScreen] scale]) * 2];
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:imageName ofType:ext];
return [UIImage imageWithData:[NSData dataWithContentsOfFile:fileLocation]];
} else {
return [UIImage imageNamed:name];
}
}
/*
* deviceAgnosticImageFromRect
*
* Returns a clipping of a "deviceAgnosticImage" (see `deviceAgnosticImageNamed`).
* Especially useful for Image-Sprites.
*
* Example:
* ```
* UImage *sprite = [UIImage deviceAgnosticImageNamed:@"myImage.png"];
* UIImage *image1 = [sprite deviceAgnosticImageFromRect:aRect];
* UIImage *image2 = [sprite deviceAgnosticImageFromRect:anotherRect];
* ```
*
*/
- (UIImage *)deviceAgnosticImageFromRect:(CGRect)frame {
// determine iPad
BOOL isIpad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
// determine factor
CGFloat factor = isIpad ? 2.0 : 1.0;
// determine resolution
uint8_t scale = [[UIScreen mainScreen] scale] * factor;
// scale frame according to the output device
frame.origin.x *= scale;
frame.origin.y *= scale;
frame.size.width *= scale;
frame.size.height *= scale;
// clip image
CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, frame);
// convert to UIImage
return [UIImage imageWithCGImage:imageRef scale:scale / factor orientation:self.imageOrientation];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment