【发布时间】:2011-11-27 09:57:40
【问题描述】:
我正在尝试进行以下批处理:
- 加载图片
- 缩放它
- 为其添加圆角边框
- 保存新的 图片为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 Cocoa 中,它不是 UIKit,到目前为止我也没有使用 CG 的东西)我不知道如何打开上面变成一个png。
【问题讨论】:
标签: cocoa core-graphics nsdata nsimage