如何压缩和缩小nsimage?

问题描述 投票:0回答:3

这是我的压缩代码
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.我想将原始图像压缩到512*512中任何人可以告诉我为什么?谢谢你
objective-c macos cocoa nsimage
3个回答
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

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

1
投票
func getImageQualityWithLevel(image: NSImage, scale: CGFloat) -> NSImage { // scale from 0.0 to 1.0 let _image = image var newRect: NSRect = NSMakeRect(0, 0, _image.size.width, _image.size.height) let imageSizeH: CGFloat = _image.size.height * scale let imageSizeW: CGFloat = _image.size.width * scale 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 }
    
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.