调整大小并保存NSImage?

Jos*_*ane 5 macos objective-c nsimage nsimageview nsopenpanel

我有一个NSImageView,我从NSOpenPanel获取图像.这很好用.

现在,我怎样才能将NSImage的大小减半,并将其保存为与原始目录相同的格式?

如果您可以随心所欲地提供帮助,谢谢.

Ann*_*nne 7

检查Matt Gemmell的ImageCrop示例项目:http:
//mattgemmell.com/source/

很好的例子如何调整大小/裁剪图像.
最后,您可以使用类似的东西来保存结果(脏样本):

// Write to TIF
[[resultImg TIFFRepresentation] writeToFile:@"/Users/Anne/Desktop/Result.tif" atomically:YES];

// Write to JPG
NSData *imageData = [resultImg  TIFFRepresentation];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.9] forKey:NSImageCompressionFactor];
imageData = [imageRep representationUsingType:NSJPEGFileType properties:imageProps];
[imageData writeToFile:@"/Users/Anne/Desktop/Result.jpg" atomically:NO];
Run Code Online (Sandbox Code Playgroud)