2

これは私の圧縮コードです

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に圧縮したいのですが、出力画像のサイズをファインダーで確認すると960×960で、元の画像と比べた場所のサイズは実際に圧縮されています。誰でも理由を教えてくれますか?ありがとうございました

4

3 に答える 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];
}

これにより、ファイルサイズがピクセル単位で縮小されます : ここからコピー

@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
于 2013-03-18T11:26:55.637 に答える
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];
于 2015-02-18T22:33:32.880 に答える