16

画像の名前、サイズ、X/Yの位置を表すオブジェクトのコレクションがあります。コレクションは「レイヤー」でソートされているので、ある種の画家のアルゴリズムで画像を合成できます。

これから、すべての画像を保持するために必要な長方形を決定できるので、次に実行したいのは次のとおりです。

  • 結果を保持するためのある種のバッファを作成します(iPhoneOSがUIGraphicsContextと呼ぶものに相当するNS)。
  • すべての画像をバッファに描画します。
  • バッファの合成結果から新しいNSImageを取得します。

iPhoneOSでは、これは私が望むことを行うコードです:

UIGraphicsBeginImageContext (woSize);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor clearColor] set];
    CGContextFillRect(ctx, NSMakeRect(0, 0, woSize.width, woSize.height));
    // draw my various images, here.
    // i.e. Various repetitions of [myImage drawAtPoint:somePoint];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

私が探しているのは、Desktop Cocoa/NSでそれを行う方法です。

ありがとう!

4

2 に答える 2

20
NSImage* resultImage = [[[NSImage alloc] initWithSize:imageSize] autorelease];
[resultImage lockFocus];

[anotherImage drawAtPoint:aPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
// Or any of the other about 6 options; see Apple's guide to pick.

[resultImage unlockFocus];

より長く、より詳細な答えについては、Appleの描画ガイドを確認してください。

于 2010-03-04T00:40:45.297 に答える
0
#import <Cocoa/Cocoa.h>

@interface CompositeView : NSView {
    NSImage *bottom;
    NSImage *top;
}
- (IBAction)takeBottomFrom: (id)aView;
- (IBAction)takeTopFrom: (id)aView;
@end

#import "CompositeView.h"

@implementation CompositeView
- (IBAction)takeBottomFrom: (id)aView
{
    id img = [[aView image] retain];
    [bottom release];
    bottom = img;
    [self setNeedsDisplay: YES];
}

- (IBAction)takeTopFrom: (id)aView
{
    id img = [[aView image] retain];
    [top release];
    top = img;
    [self setNeedsDisplay: YES];
}

- (void)drawRect:(NSRect)rect
{
    NSCompositingOperation op = 0;
    NSRect bounds = [self bounds];
    NSSize imageSize = bounds.size;
    imageSize.width /= 7;
    imageSize.height /= 2;

    NSRect bottomRect = { {0,0}, [bottom size] };
    NSRect topRect = { {0,0}, [top size] };

    for (unsigned y=0 ; y<2 ; y++)
    {
        for (unsigned x=0 ; x<7 ; x++)
        {
            NSRect drawRect;

            drawRect.origin.y = y * imageSize.height;
            drawRect.origin.x = x * imageSize.width;
            drawRect.size = imageSize;

            [bottom drawInRect: drawRect
                      fromRect: bottomRect
                     operation: NSCompositeCopy
                      fraction: 1];

            [top drawInRect: drawRect
                   fromRect: topRect
                  operation: op++
                   fraction: 1];
        }
    }
}

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
    return self;
}

@end
于 2015-09-15T09:49:11.290 に答える