2013-03-18 114 views
1

這是我的壓縮碼如何壓縮和縮小NSImage?

NSBitmapImageRep* tmpRep = [[_image representations] objectAtIndex:0]; 
[tmpRep setPixelsWide:512]; 
[tmpRep setPixelsHigh:512]; 
[tmpRep setSize:NSMakeSize(SmallThumbnailWidth, SmallThumbnailHeight)]; 
NSDictionary* imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.3] forKey:NSImageCompressionFactor]; 
NSData* outputImageData = [tmpRep 
          representationUsingType:NSJPEGFileType properties:imageProps]; 
NSString* imageFilePath = [NSString stringWithFormat:@"%@/thumbnail.jpg",imagePath]; 
[outputImageData writeToFile:imageFilePath atomically:YES]; 

原始圖像尺寸是960 * 960.I想將原始圖像壓縮成512 * 512.But輸出圖像的尺寸爲960×960,當我在取景器檢查和原來相比的位置大小真的被壓縮了。任何人都可以告訴我爲什麼?謝謝

回答

8

試試這個:

這將減少在KBS保存大小:

-(NSImage *)imageCompressedByFactor:(float)factor{ 
    NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithData:[self TIFFRepresentation]]; 
    NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:factor] forKey:NSImageCompressionFactor]; 
    NSData *compressedData = [imageRep representationUsingType:NSJPEGFileType properties:options]; 
    return [[NSImage alloc] initWithData:compressedData]; 
} 

這將減少像素文件大小: here

@implementation NSImage (ProportionalScaling) 

- (NSImage*)imageByScalingProportionallyToSize:(NSSize)targetSize{ 
    NSImage* sourceImage = self; 
    NSImage* newImage = nil; 

    if ([sourceImage isValid]){ 
    NSSize imageSize = [sourceImage size]; 
    float width = imageSize.width; 
    float height = imageSize.height; 

    float targetWidth = targetSize.width; 
    float targetHeight = targetSize.height; 

    float scaleFactor = 0.0; 
    float scaledWidth = targetWidth; 
    float scaledHeight = targetHeight; 

    NSPoint thumbnailPoint = NSZeroPoint; 

    if (NSEqualSizes(imageSize, targetSize) == NO) 
    { 

     float widthFactor = targetWidth/width; 
     float heightFactor = targetHeight/height; 

     if (widthFactor < heightFactor) 
     scaleFactor = widthFactor; 
     else 
     scaleFactor = heightFactor; 

     scaledWidth = width * scaleFactor; 
     scaledHeight = height * scaleFactor; 

     if (widthFactor < heightFactor) 
     thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 

     else if (widthFactor > heightFactor) 
     thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; 
    } 
    newImage = [[NSImage alloc] initWithSize:targetSize]; 
    [newImage lockFocus]; 
     NSRect thumbnailRect; 
     thumbnailRect.origin = thumbnailPoint; 
     thumbnailRect.size.width = scaledWidth; 
     thumbnailRect.size.height = scaledHeight; 
     [sourceImage drawInRect: thumbnailRect 
        fromRect: NSZeroRect 
        operation: NSCompositeSourceOver 
        fraction: 1.0]; 
    [newImage unlockFocus]; 
    } 
    return [newImage autorelease]; 
} 
@end 
複製
+0

真棒,只是需要這個,'NSImage'是如此與'UIImage'不同,這爲我節省了一小時的研究時間! – 2013-09-29 12:01:08

+0

嗨@ H2CO3:你跑到95K。我在過去6個月的項目中非常忙碌......因此無法給出單個答案:( – 2013-09-29 12:20:26

1

像這樣創建類別方法,以便遞增壓縮圖像直到您滿足所需的文件大小:

- (NSImage*) compressUnderMegaBytes:(CGFloat)megabytes { 
    CGFloat compressionRatio = 1.0; 

    NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithData:[self TIFFRepresentation]]; 
    NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:compressionRatio] forKey:NSImageCompressionFactor]; 
    NSData *compressedData = [imageRep representationUsingType:NSJPEGFileType properties:options]; 

    while ([compressedData length]>(megabytes*1024*1024)) { 
    @autoreleasepool { 
     compressionRatio = compressionRatio * 0.9; 
     options = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:compressionRatio] forKey:NSImageCompressionFactor]; 
     compressedData = [imageRep representationUsingType:NSJPEGFileType properties:options]; 

     // Safety check, 0.4 is a reasonable compression size, anything below will become blurry 
     if (compressionRatio <= 0.4) { 
     break; 
     } 
    } 
    } 
    return [[NSImage alloc] initWithData: compressedData]; 
} 

然後,您可以使用它像這樣:

NSImage *compressedImage = [myImage compressUnderMegaBytes: 0.5]; 
+0

謝謝你,我已經解決了這個問題。 – gohamgx 2015-02-27 11:08:03

0

水平從0.0到1.0

func getImageQualityWithLevel(image: NSImage, level: CGFloat) -> NSImage { 
    let _image = image 
    var newRect: NSRect = NSMakeRect(0, 0, _image.size.width, _image.size.height) 

    let imageSizeH: CGFloat = _image.size.height * level 
    let imageSizeW: CGFloat = _image.size.width * level 


    var newImage = NSImage(size: NSMakeSize(imageSizeW, imageSizeH)) 
    newImage.lockFocus() 
    NSGraphicsContext.currentContext()?.imageInterpolation = NSImageInterpolation.Low 


    _image.drawInRect(NSMakeRect(0, 0, imageSizeW, imageSizeH), fromRect: newRect, operation: NSCompositingOperation.CompositeSourceOver, fraction: 1) 
    newImage.unlockFocus() 


    return newImage 
}