代码之家  ›  专栏  ›  技术社区  ›  Olie

如何将多个nsImages合成一个大图像?

  •  15
  • Olie  · 技术社区  · 16 年前

    我有一组描述图像名称、大小和X/Y位置的对象。集合是按“层”排序的,所以我可以用一种画家的算法合成图像。

    由此,我可以确定保存所有图像所需的矩形,因此现在我要做的是:

    • 创建某种缓冲区来保存结果(ns相当于iphoneos所称的uigraphicContext)。
    • 将所有图像绘制到缓冲区中。
    • 从缓冲区的合成结果中截取一个新的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();
    

    我要找的是如何在桌面cocoa/ns中做到这一点。

    谢谢!

    2 回复  |  直到 10 年前
        1
  •  19
  •   andyvn22    16 年前
    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's Drawing Guide 更长的时间,更详细的答案。

        2
  •  0
  •   Durul Dalkanat    10 年前
    #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
    
    推荐文章