2011-11-27 53 views
1

我试图做如下的批处理:如何将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?)];并将该上下文保存到磁盘。但我很困惑,:

什么情况下,

是否从上下文中去NSData的磁盘,

,或者如果我保存在什么地方需要一个中间图像。基本上,CALayers和CGLayers和NS对象和UI对象之间(我知道我在OS X可可在这里,它没有UIKit,我也没有使用CG的东西)我不知道如何转向以上变成了png。

回答

2

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

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

下面是如何你想使用什么可可图纸做了一个例子:

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); 
} 
+0

其实我可能会去的石英的方法,因为它更像是OpenGL的哪个我更舒服。但这种方式也起作用。谢谢! – SaldaVonSchwartz