如何将NSImageView图层保存为磁盘上的PNG格式



我正在尝试做以下批处理:

  1. 加载图片
  2. 规模它
  3. 添加圆角边框
  4. 保存新的图片格式为PNG

到目前为止,我想到了这个:

CGPoint center = CGPointMake(self.window.frame.size.width / 2., self.window.frame.size.height / 2.);
      [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
      NSImage * newThumbnail = [[NSImage alloc] initWithData:[NSData dataWithContentsOfURL:[files objectAtIndex:0]]];
      NSImageView *imageView = [[NSImageView alloc] initWithFrame:CGRectMake(center.x - (57/2.), center.y - (80/2.), 57, 80)];
      [imageView setWantsLayer:YES];
      imageView.layer.borderColor = [[NSColor blackColor] CGColor];
      imageView.layer.cornerRadius = 4;
      imageView.layer.masksToBounds = YES;
      imageView.layer.borderWidth = 1.0;
      imageView.image = newThumbnail;

现在我想我错过了告诉图层渲染到一些上下文?比如[imageView.layer drawInContext: some context (NS o CG?)];,然后把上下文保存到磁盘上。但是我很困惑:

what context,

是否从context转到NSData转到disk,

或者在保存之前需要一个中间图像。

基本上,在calayer和cglayer和NS对象和UI对象之间(我知道我在OS X Cocoa这里没有UIKit,我也没有使用CG的东西到目前为止)我不知道如何把上面变成png.

您根本不应该使用视图,因为您不需要显示到屏幕上。您应该使用屏幕外的位图(NSBitmapImageRep)并使用绘图命令来绘制图像。

在Cocoa中绘图有两种选择。最简单的方法是使用各种可可绘图类,如NSBezierPath和朋友。另一种选择是更强大但也更低级和复杂的Quartz api,它不是面向对象的,而是使用C函数语法。

下面是一个如何使用Cocoa绘图的示例:

NSImage* anImage = [NSImage imageNamed:@"Lenna.tiff"]; //or some other source
//create a bitmap at a specific size
NSRect offscreenRect = NSMakeRect(0.0, 0.0, 250.0, 250.0);
NSBitmapImageRep* bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil
                                                       pixelsWide:offscreenRect.size.width
                                                       pixelsHigh:offscreenRect.size.height
                                                    bitsPerSample:8
                                                  samplesPerPixel:4
                                                         hasAlpha:YES
                                                         isPlanar:NO
                                                   colorSpaceName:NSCalibratedRGBColorSpace
                                                     bitmapFormat:0
                                                      bytesPerRow:(4 * offscreenRect.size.width)
                                                     bitsPerPixel:32];
//save the current graphics context and lock focus on the bitmap
NSGraphicsContext* originalContext = [NSGraphicsContext currentContext];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext
                                      graphicsContextWithBitmapImageRep:bitmap]];
[NSGraphicsContext saveGraphicsState];
//clear the image rep. This is faster than filling with [NSColor clearColor].
unsigned char *bitmapData = [bitmap bitmapData];
if (bitmapData)
    bzero(bitmapData, [bitmap bytesPerRow] * [bitmap pixelsHigh]);
//create the border path
CGFloat borderWidth = 2.0;
CGFloat cornerRadius = 14.0;
NSRect borderRect = NSInsetRect(offscreenRect, borderWidth/2.0, borderWidth/2.0);
NSBezierPath* border = [NSBezierPath bezierPathWithRoundedRect:borderRect xRadius:cornerRadius yRadius:cornerRadius];
[border setLineWidth:borderWidth];
//set the border as a clipping path
[NSGraphicsContext saveGraphicsState];
[border addClip];
//scale and draw the image
[anImage setSize:offscreenRect.size];
[anImage drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
//set the border color
[[NSColor blackColor] set];
//draw the border
[border stroke];
//restore the original graphics context
[NSGraphicsContext restoreGraphicsState];
[NSGraphicsContext setCurrentContext:originalContext];
//get PNG data from the image rep
NSData* pngData = [bitmap representationUsingType:NSPNGFileType properties:nil];
NSError* error;
if(![pngData writeToURL:[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"test.png"]] options:NSDataWritingAtomic error:&error])
{
    NSLog(@"%@",error);
}

最新更新