Skip to content

Instantly share code, notes, and snippets.

@rsaunders100
Created October 4, 2012 17:25
Show Gist options
  • Save rsaunders100/3835112 to your computer and use it in GitHub Desktop.
Save rsaunders100/3835112 to your computer and use it in GitHub Desktop.
Transform an image threadsafe
//
// 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
//
// 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