How to composite several NSImages into one big image?

后端 未结 2 514
北恋
北恋 2021-02-13 02:51

I have a collection of objects which describe an image-name, its size and it\'s X/Y location. The collection is sorted by \"layers\", so I can composite the images in a sort of

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-13 03:08

    #import 
    
    @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
    

提交回复
热议问题