이미지 회전할 일이 많아서 많이 쓰는데
매번 프로젝트 열어서 찾기 귀찮아서 작성
#pragma mark - rotateImage
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
- (UIImage *)rotateImage90:(UIImage *)img
{
NSLog(@"rotateImage90:");
CGImageRef imgRef = img.CGImage;
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGRect bounds = CGRectMake(0, 0, width, height);
CGFloat boundHeight;
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * bounds.size.width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(nil, bounds.size.width, bounds.size.height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextRotateCTM (context, DEGREES_TO_RADIANS(270));
CGContextTranslateCTM (context, -width, 0);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imgRef);
CGImageRef newImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage *imageCopy = [UIImage imageWithCGImage:newImage];
CFRelease(newImage);
return imageCopy;
}
- (UIImage *)rotateImageReverse90:(UIImage *)img
{
NSLog(@"rotateImageReverse90:");
CGImageRef imgRef = img.CGImage;
CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);
CGRect bounds = CGRectMake(0, 0, width, height);
CGFloat boundHeight;
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * bounds.size.width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(nil, bounds.size.width, bounds.size.height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextRotateCTM (context, DEGREES_TO_RADIANS(90));
CGContextTranslateCTM (context, 0, -height);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imgRef);
CGImageRef newImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage *imageCopy = [UIImage imageWithCGImage:newImage];
CFRelease(newImage);
return imageCopy;
}
'프로그래밍 > iOS' 카테고리의 다른 글
[iOS] 카메라해상도와 이미지와의 관계 (0) | 2016.10.14 |
---|---|
import (0) | 2016.10.05 |
[iOS] camera iOS10 crash (0) | 2016.09.21 |
[iOS] 버튼 멀티터치 막기 (0) | 2016.08.23 |
[iOS] 시스템 언어 가져오기 (0) | 2016.08.22 |