본문 바로가기

iPhone dev./Objective C general

UIImage 용량 줄이기

UIImage에 imageWithCGImage: scale: orientation: 이라는 함수가 있지만, scale로 크기만 조절할 수 있을 뿐, 이를 NSData로 옮겨도 용량은 그대로이다.

그림의 실질적인 용량이 줄기위해선 UIGraphics... 함수로 image를 다시 그려야 한다.

다음 예제 코드를 참고한다:

(http://stackoverflow.com/questions/5012695/how-to-reduce-the-uiimage-size)


            UIImage *image = [actualImage you want to resize];
            UIImage *tempImage = nil;
            CGSize targetSize = CGSizeMake(196,110);
            UIGraphicsBeginImageContext(targetSize);

            CGRect thumbnailRect = CGRectMake(0, 0, 0, 0);
            thumbnailRect.origin = CGPointMake(0.0,0.0);
            thumbnailRect.size.width  = targetSize.width;
            thumbnailRect.size.height = targetSize.height;

            [image drawInRect:thumbnailRect];

            tempImage = UIGraphicsGetImageFromCurrentImageContext();

            UIGraphicsEndImageContext();


UIImage *image는 원래 그림, *tempImage는 변환할 그림, targetSize는 줄어든 그림의 크기(가로, 세로 길이)이다.