본문 바로가기

프로그래밍/iOS

[iOS] UIImage 회전 함수

자주사용하는 함수인데 이미지 rotation을 받아서 회전하는 함수만 적어두었길래

그냥 가져다 쓸 수 있게 함수 형식으로 바꾼 샘플 코드 작성


#pragma mark - rotateImage

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)

/**

 오른쪽 90도로 돌리는 함수

*/

- (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;

}


/**

 왼쪽 90도로 돌리는 함수

*/

- (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] Annotation View  (0) 2016.07.29
[iOS] 다국어가 적용되지 않는 언어를 선택할 시  (0) 2016.07.01
[iOS] CABasicAnimation scale  (0) 2016.06.14
[iOS] 원 모양 뷰 만들기  (0) 2016.06.14
[iOS] token animation  (0) 2016.06.14