NSImage から CGIImageRef を取得する必要があります。Cocoa for Mac OS X でこれを行う簡単な方法はありますか?
7097 次
2 に答える
15
見逃すのはかなり難しい:
于 2009-11-11T01:36:41.717 に答える
5
Mac OS X 10.5 またはその他の以前のリリースをターゲットにする必要がある場合は、代わりに次のスニペットを使用してください。そうでない場合は、NSDの答えが正しい方法です。
CGImageRef CGImageCreateWithNSImage(NSImage *image) {
NSSize imageSize = [image size];
CGContextRef bitmapContext = CGBitmapContextCreate(NULL, imageSize.width, imageSize.height, 8, 0, [[NSColorSpace genericRGBColorSpace] CGColorSpace], kCGBitmapByteOrder32Host|kCGImageAlphaPremultipliedFirst);
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:bitmapContext flipped:NO]];
[image drawInRect:NSMakeRect(0, 0, imageSize.width, imageSize.height) fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);
CGContextRelease(bitmapContext);
return cgImage;
}
画像がファイルから取得されている場合は、画像ソースを使用してデータを直接 CGImageRef にロードする方がよい場合があります。
于 2009-11-11T15:00:54.913 に答える