使用Cocoa减少图像字节大小

时间:2010-02-22 19:13:16

标签: objective-c cocoa

我有一个1600x1600的1.2MB图像,其大小调整为320x320缩小到404KB。 我需要进一步减少字节大小而不减小图像宽高比。

目前我正在使用TIFFRepresentationUsingCompression:factor: NSImage方法和NSTIFFCompressionJPEG以及不会影响图像尺寸/质量的因素。

我该如何解决?

2 个答案:

答案 0 :(得分:7)

如果您需要压缩并且不介意丢失图像质量,请将文件另存为JPEG。为此,您需要从图像中获取NSBitmapImageRep,然后获得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];

答案 1 :(得分: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