Cocoa,Objective-C如何调整PNG图像的大小?

5

我是cocoa的新手,我想要做的是调整NSImage的大小,但是看起来我做错了什么,因为我的图像没有被调整大小。我查看了其他问题,并且这种方法看起来应该可以工作:

- (void)scaleIcons:(NSString *)outputPath{
NSImage *anImage;
NSSize imageSize;
NSString *finalPath;

anImage = [self image];

imageSize = [anImage size];
imageSize.width = 512;
imageSize.height = 512;
[anImage setSize:imageSize];

finalPath = [outputPath stringByAppendingString:@"/icon_512x512.png"];

NSData *imageData = [anImage TIFFRepresentation];
NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:imageData];
NSData *dataToWrite = [rep representationUsingType:NSPNGFileType properties:nil];
[dataToWrite writeToFile:finalPath atomically:NO];
}

一切都正常,唯独我的图片没有被缩放。有人可以帮忙吗?
3个回答

6
所以你的代码片段忽略了实际需要进行缩放/转换图像的操作。在尝试保存图像之前,请通过此方法将图像传递处理一下:
- (NSImage *)scaleImage:(NSImage *)image toSize:(NSSize)targetSize
{
  if ([image isValid])
  {
    NSSize imageSize = [image 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))
    {
      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;
      }

    NSImage *newImage = [[NSImage alloc] initWithSize:targetSize];

    [newImage lockFocus];

     NSRect thumbnailRect;
     thumbnailRect.origin = thumbnailPoint;
     thumbnailRect.size.width = scaledWidth;
     thumbnailRect.size.height = scaledHeight;

     [image drawInRect:thumbnailRect
              fromRect:NSZeroRect
             operation:NSCompositeSourceOver
              fraction:1.0];

    [newImage unlockFocus];
  }

  return newImage;
}

如果您将此集成到现有的方法中:
- (void)scaleIcons:(NSString *)outputPath{

    NSSize outputSize = NSMakeSize(512.0f,512.0f);
    NSImage *anImage  = [self scaleImage:[self image] toSize:outputSize];

    NSString *finalPath = [outputPath stringByAppendingString:@"/icon_512x512.png"];
    NSData *imageData = [anImage TIFFRepresentation];
    NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:imageData];
    NSData *dataToWrite = [rep representationUsingType:NSPNGFileType properties:nil];
   [dataToWrite writeToFile:finalPath atomically:NO];
}

感谢您提供如此详细的答案!我只需要更改这一行代码:NSSize outputSize = NSSizeMake(512.0f,512.0f); 替换为:NSSize outputSize = NSSizeFromString(@"{512,512}"); - Rokas
1
NSMakeSize是你想要的。NSSizeFromString会执行大量不必要的字符串解析。 - Catfish_Man
已更新答案,之前犯了一个错误,因为我大部分的经验都是在Cocoa Touch上(在那里你会调用CGSizeMake(width,height))...希望这对你有用! - isaac

2
关于Parag Bafna的回答,对于调整大小应该使用“重新采样”而不是“裁剪”:
sips --resampleHeightWidth 10 10 input.png --out output.png

0

您还可以使用sips(可脚本化图像处理系统)命令。

sips --cropToHeightWidth 10 10 input.png --out output.png

那个命令的名称暗示它会裁剪图像(修剪不在所选区域内的像素),而不是调整大小。 - Duncan C

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接