CoreImage:创建一个纯色的CIImage并在NSImageView中显示它

时间:2011-11-05 02:52:40

标签: objective-c cocoa core-image

我想以编程方式生成带有纯色填充的矩形图像。 我怎样才能做到这一点? 我想用inputImage填充inputImageView。

到目前为止,这是我的代码,但我得到的只是一个空图像视图:

-(void) awakeFromNib{
  CIColor *red = [CIColor colorWithRed:1 green:0 blue:0];
  CIImage *redImage = [CIImage imageWithColor:red];

  NSCIImageRep *ir = [NSCIImageRep imageRepWithCIImage:redImage];

  [self setInputImage:[[NSImage alloc] initWithSize: NSMakeSize(25, 25)]];
  //magic should be happening right here!
  [[self inputImage] addRepresentation:ir];
  [[self inputView] setImage:[self inputImage]];
  [inputView setNeedsDisplay:YES];
}

1 个答案:

答案 0 :(得分:11)

NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(25, 25)];
[image lockFocus];
[[NSColor redColor] setFill];
[NSBezierPath fillRect:NSMakeRect(0, 0, 25, 25)];
[image unlockFocus];