質問

NSImage から CGIImageRef を取得する必要があります。Cocoa for Mac OS X でこれを行う簡単な方法はありますか?

役に立ちましたか?

解決

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 に直接ロードします。

他のヒント

欠場するかなり難しいます:

<のhref = "http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSImage_Class/Reference/Reference.html#//apple_ref/doc/uid/20000344- SW109" REL = "noreferrer"> - [NSImageではCGImageForProposedRect:コンテキスト:ヒント:] の

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top