2010-02-22 7 views
4

320x320 크기로 조정 된 1600x1600 1.2MB 이미지의 크기가 404KB로 줄어 듭니다. 이미지 가로 세로 크기를 줄이지 않고도 바이트 크기를 줄여야합니다.코코아로 이미지 바이트 크기 줄이기

현재 NSTIFFCompressionJPEG과 함께 NSImage 메서드를 사용하고 있으며 이미지 크기/품질에 영향을 미치지 않는 요소가 있습니다.

어떻게 해결할 수 있습니까? 마르코

+0

을 얘기하는 이렇게하려면, 당신은 당신의 이미지에서 NSBitmapImageRep를 얻을 후 JPEG 표현을 얻을 필요가? –

+0

디스크에 있습니다. 1.2MB 및 404KB는 디스크 크기입니다 – Marco

답변

7

당신은 압축을해야 할 경우 당신은 다음 JPEG 파일로 저장 이미지 품질을 잃고 신경 쓰지 않는다. 당신이 메모리 나 디스크에 이미지 크기를 줄일 수에 대해

//yourImage is an NSImage object 

NSBitmapImageRep* myBitmapImageRep; 

if(useActualSize) 
{ 
    //this will produce an image the full size of the NSImage's backing bitmap, which may not be what you want 
    myBitmapImageRep = [NSBitmapImageRep imageRepWithData: [yourImage TIFFRepresentation]]; 
} 
else 
{ 
    //this will get a bitmap from the image at 1 point == 1 pixel, which is probably what you want 
    NSSize imageSize = [yourImage size]; 
    [yourImage lockFocus]; 
    NSRect imageRect = NSMakeRect(0, 0, imageSize.width, imageSize.height); 
    myBitmapImageRep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect] autorelease]; 
    [yourImage unlockFocus]; 
} 

CGFloat imageCompression = 0.7; //between 0 and 1; 1 is maximum quality, 0 is maximum compression 

// set up the options for creating a JPEG 
NSDictionary* jpegOptions = [NSDictionary dictionaryWithObjectsAndKeys: 
       [NSNumber numberWithDouble:imageCompression], NSImageCompressionFactor, 
       [NSNumber numberWithBool:NO], NSImageProgressive, 
       nil]; 

// get the JPEG encoded data 
NSData* jpegData = [myBitmapImageRep representationUsingType:NSJPEGFileType properties:jpegOptions]; 
//write it to disk 
[jpegData writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"foo.jpg"] atomically:YES]; 
+0

이미지 화질은 괜찮지 만 코드는 -TIFFRepresentationUsingCompression : factor : method와 동일하지 않습니다. – Marco

+1

아니요.이 메서드는 내부 JPEG 압축을 사용하여 TIFF 파일을 만듭니다. 그러나 문서에는 "TIFF 파일의 JPEG 압축은 지원되지 않으며 요인은 무시됩니다."라고되어 있습니다. 내 코드는 실제 JPEG 파일을 만듭니다. –

+0

누락 된'autorelease'를 찾아 주셔서 감사합니다, 피터, 필자는 GC 프로젝트에서 내 범주의 해당 행을 복사 했으므로 필요하지 않습니다. –

0
#import "UIImage+Compress.h" 

#define MAX_IMAGEPIX 200.0   // max pix 200.0px 
#define MAX_IMAGEDATA_LEN 50000.0 // max data length 5K 

@implementation UIImage (Compress) 

- (UIImage *)compressedImage { 
    CGSize imageSize = self.size; 
    CGFloat width = imageSize.width; 
    CGFloat height = imageSize.height; 

    if (width <= MAX_IMAGEPIX && height <= MAX_IMAGEPIX) { 
     // no need to compress. 
     return self; 
    } 

    if (width == 0 || height == 0) { 
     // void zero exception 
     return self; 
    } 

    UIImage *newImage = nil; 
    CGFloat widthFactor = MAX_IMAGEPIX/width; 
    CGFloat heightFactor = MAX_IMAGEPIX/height; 
    CGFloat scaleFactor = 0.0; 

    if (widthFactor > heightFactor) 
     scaleFactor = heightFactor; // scale to fit height 
    else 
     scaleFactor = widthFactor; // scale to fit width 

    CGFloat scaledWidth = width * scaleFactor; 
    CGFloat scaledHeight = height * scaleFactor; 
    CGSize targetSize = CGSizeMake(scaledWidth, scaledHeight); 

    UIGraphicsBeginImageContext(targetSize); // this will crop 

    CGRect thumbnailRect = CGRectZero; 
    thumbnailRect.size.width = scaledWidth; 
    thumbnailRect.size.height = scaledHeight; 

    [self drawInRect:thumbnailRect]; 

    newImage = UIGraphicsGetImageFromCurrentImageContext(); 

    //pop the context to get back to the default 
    UIGraphicsEndImageContext(); 

    return newImage; 

} 

- (NSData *)compressedData:(CGFloat)compressionQuality { 
    assert(compressionQuality <= 1.0 && compressionQuality >= 0); 

    return UIImageJPEGRepresentation(self, compressionQuality); 
} 

- (CGFloat)compressionQuality { 
    NSData *data = UIImageJPEGRepresentation(self, 1.0); 
    NSUInteger dataLength = [data length]; 

    if(dataLength > MAX_IMAGEDATA_LEN) { 
     return 1.0 - MAX_IMAGEDATA_LEN/dataLength; 
    } else { 
     return 1.0; 
    } 
} 

- (NSData *)compressedData { 
    CGFloat quality = [self compressionQuality]; 

    return [self compressedData:quality]; 
} 

@end 
관련 문제