Created
October 4, 2012 17:25
-
-
Save rsaunders100/3835112 to your computer and use it in GitHub Desktop.
Transform an image threadsafe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIImage+Transform.h | |
// TestImage | |
// | |
// Created by Robert Saunders on 04/10/2012. | |
// Copyright (c) 2012 Robert Saunders. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIImage (Transform) | |
// Thread-safe | |
- (UIImage*) imageByApplyingTransfromAboutCenter:(CGAffineTransform)transform; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIImage+Transform.m | |
// TestImage | |
// | |
// Created by Robert Saunders on 04/10/2012. | |
// Copyright (c) 2012 Robert Saunders. All rights reserved. | |
// | |
#import "UIImage+Transform.h" | |
@implementation UIImage (Transform) | |
- (UIImage*) imageByApplyingTransfromAboutCenter:(CGAffineTransform)transform | |
{ | |
CGFloat imageWidth = self.size.width; | |
CGFloat imageHeight = self.size.height; | |
CGImageRef imageRef = [self CGImage]; | |
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); | |
CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB(); | |
bitmapInfo = kCGImageAlphaNone; | |
CGContextRef bitmap = | |
CGBitmapContextCreate(NULL, | |
imageWidth , | |
imageHeight, | |
8, | |
(4 * self.size.width), | |
colorSpaceInfo, | |
kCGImageAlphaPremultipliedFirst); | |
CGContextTranslateCTM(bitmap, imageWidth / 2 , imageHeight / 2); | |
CGContextConcatCTM (bitmap, transform); | |
CGRect rectForDrawing = CGRectMake(-imageWidth / 2 , -imageHeight / 2, imageWidth, imageHeight); | |
CGContextDrawImage(bitmap, rectForDrawing, imageRef); | |
CGImageRef ref = CGBitmapContextCreateImage(bitmap); | |
UIImage* newImage = [UIImage imageWithCGImage:ref]; | |
CGContextRelease(bitmap); | |
CGImageRelease(ref); | |
return newImage; | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment