13

私は iOS で 2 年間プログラミングしていますが、Mac ではプログラミングをしたことがありません。私は、iOS 開発で必要ないくつかの単純な画像を処理するための小さなユーティリティに取り組んでいます。とにかく、私は iOS で完全に動作する作業コードを持っていますが、Mac に相当するものはまったくわかりません。

さまざまなことを試しましたが、「drawRect:」メソッドの外で Mac でグラフィックス コンテキストを開始する方法が本当にわかりません。iPhone では、UIGraphicsBeghinImageContext() を使用します。他の投稿で lockFocus/unlockFocus を使用するように言われていることは知っていますが、それを自分のニーズに合わせて正確に機能させる方法がわかりません。ああ、UIImage の「CGImage」プロパティが本当に恋しいです。NSImage がそれを持てない理由がわかりませんが、それよりも少しトリッキーに聞こえます。

iOS での作業コードは次のとおりです。基本的には、マスクから反射イメージを作成し、それらを結合するだけです。

    UIImage *mask = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Mask_Image.jpg" ofType:nil]];
    UIImage *image = [UIImage imageNamed::@"Test_Image1.jpg"];
    UIGraphicsBeginImageContextWithOptions(mask.size, NO, [[UIScreen mainScreen]scale]);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(ctx, 0.0, mask.size.height);
    CGContextScaleCTM(ctx, 1.f, -1.f);
    [image drawInRect:CGRectMake(0.f, -mask.size.height, image.size.width, image.size.height)];
    UIImage *flippedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    CGImageRef maskRef = mask.CGImage;
    CGImageRef maskCreate = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                              CGImageGetHeight(maskRef),
                                              CGImageGetBitsPerComponent(maskRef),
                                              CGImageGetBitsPerPixel(maskRef),
                                              CGImageGetBytesPerRow(maskRef),
                                              CGImageGetDataProvider(maskRef), NULL, false);
    CGImageRef masked = CGImageCreateWithMask([flippedImage CGImage], maskCreate);

    CGImageRelease(maskCreate);

    UIImage *maskedImage = [UIImage imageWithCGImage:masked];
    CGImageRelease(masked);

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(image.size.width, image.size.height + (image.size.height * .5)), NO, [[UIScreen mainScreen]scale]);

    [image drawInRect:CGRectMake(0,0, image.size.width, image.size.height)];
    [maskedImage drawInRect:CGRectMake(0, image.size.height, maskedImage.size.width, maskedImage.size.height)];

    UIImage *anotherImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext(); 

//do something with anotherImage

Mac でこれを (単純に) 達成するための提案はありますか?

4

4 に答える 4

23

青色の円を NSImage に描画する簡単な例を次に示します (この例では ARC を使用しています。好みに合わせて保持/リリースを追加します)。

NSSize size = NSMakeSize(50, 50);

NSImage* im = [[NSImage alloc] initWithSize:size];
NSBitmapImageRep* rep = [[NSBitmapImageRep alloc]
                            initWithBitmapDataPlanes:NULL
                                          pixelsWide:size.width
                                          pixelsHigh:size.height
                                       bitsPerSample:8
                                     samplesPerPixel:4
                                            hasAlpha:YES
                                            isPlanar:NO
                                      colorSpaceName:NSCalibratedRGBColorSpace
                                         bytesPerRow:0
                                        bitsPerPixel:0];

[im addRepresentation:rep];

[im lockFocus];

CGContextRef ctx = [[NSGraphicsContext currentContext] graphicsPort];
CGContextClearRect(ctx, NSMakeRect(0, 0, size.width, size.height));
CGContextSetFillColorWithColor(ctx, [[NSColor blueColor] CGColor]);
CGContextFillEllipseInRect(ctx, NSMakeRect(0, 0, size.width, size.height));

[im unlockFocus];

[[im TIFFRepresentation] writeToFile:@"/Users/USERNAME/Desktop/foo.tiff" atomically:NO];

主な違いは、OS X では最初にイメージを作成する必要があり、それから描画を開始できることです。iOS では、コンテキストを作成してから、そこから画像を抽出します。

基本的に、lockFocus は現在のコンテキストを画像にし、その上に直接描画してから画像を使用します。

これがあなたの質問のすべてに答えているかどうかは完全にはわかりませんが、少なくともその一部だと思います.

于 2012-09-04T19:50:13.137 に答える
10

さて、ここにメモがありUIGraphicsBeginImageContextWithOptionsます:

UIGraphicsBeginImageContextWithOptions

指定されたオプションを使用して、ビットマップ ベースのグラフィックス コンテキストを作成します。

iOSでも利用できる(UIGraphicsBeginImageContextWithOptionsおそらくラッパーである)OS Xの同等物は次のCGBitmapContextCreateとおりです。

次のように宣言されています。

CGContextRef CGBitmapContextCreate (
   void *data,
   size_t width,
   size_t height,
   size_t bitsPerComponent,
   size_t bytesPerRow,
   CGColorSpaceRef colorspace,
   CGBitmapInfo bitmapInfo
);

これは C API ですが、CGBitmapContext は CGContext のサブクラスと考えることができます。これはピクセル バッファーにレンダリングされますが、CGContext は抽象的な宛先にレンダリングされます。

の場合、ビットマップ コンテキストをUIGraphicsGetImageFromCurrentImageContext使用CGBitmapContextCreateImageして渡し、CGImage を作成できます。

于 2012-09-06T03:47:22.263 に答える
9

Cobbalの回答のSwift(2.1 / 10.11 API準拠)バージョンは次のとおりです

    let size = NSMakeSize(50, 50);
    let im = NSImage.init(size: size)

    let rep = NSBitmapImageRep.init(bitmapDataPlanes: nil,
        pixelsWide: Int(size.width),
        pixelsHigh: Int(size.height),
        bitsPerSample: 8,
        samplesPerPixel: 4,
        hasAlpha: true,
        isPlanar: false,
        colorSpaceName: NSCalibratedRGBColorSpace,
        bytesPerRow: 0,
        bitsPerPixel: 0)

    im.addRepresentation(rep!)
    im.lockFocus()

    let rect = NSMakeRect(0, 0, size.width, size.height)
    let ctx = NSGraphicsContext.currentContext()?.CGContext
    CGContextClearRect(ctx, rect)
    CGContextSetFillColorWithColor(ctx, NSColor.blackColor().CGColor)
    CGContextFillRect(ctx, rect)

    im.unlockFocus()
于 2015-12-18T17:46:58.507 に答える
4

コバルの答えのSwift3バージョン

   let size = NSMakeSize(50, 50);
    let im = NSImage.init(size: size)

    let rep = NSBitmapImageRep.init(bitmapDataPlanes: nil,
                                    pixelsWide: Int(size.width),
                                    pixelsHigh: Int(size.height),
                                    bitsPerSample: 8,
                                    samplesPerPixel: 4,
                                    hasAlpha: true,
                                    isPlanar: false,
                                    colorSpaceName: NSCalibratedRGBColorSpace,
                                    bytesPerRow: 0,
                                    bitsPerPixel: 0)


   im.addRepresentation(rep!)
    im.lockFocus()

    let rect = NSMakeRect(0, 0, size.width, size.height)
    let ctx = NSGraphicsContext.current()?.cgContext
    ctx!.clear(rect)
    ctx!.setFillColor(NSColor.black.cgColor)
    ctx!.fill(rect)

    im.unlockFocus()
于 2017-05-10T13:04:50.297 に答える