如何将NSImageView图层保存到磁盘上的PNG?

时间:2011-11-27 09:57:41

标签: cocoa core-graphics nsdata nsimage

我正在尝试进行以下批处理:

  1. 加载图片
  2. 缩放
  3. 为其添加圆角边框
  4. 保存新的     图像为PNG
  5. 到目前为止,我提出了这个问题:

    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?)];并将该上下文保存到磁盘。但我很困惑:

    什么情境,

    是否从上下文转到NSData到磁盘,

    或者如果我在保存之前需要某个中间图像。

    基本上,在CALayers和CGLayers之间以及NS对象和UI对象之间(我知道我在OS X Cocoa这里并没有UIKit,到目前为止我也没有使用CG的东西)我不知道怎么转上面写着一个png。

1 个答案:

答案 0 :(得分:2)

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

在Cocoa中有两种绘图选择。最简单的方法是使用各种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);
}