Question

J'ai une collection d'objets qui décrivent un nom d'image, sa taille et son emplacement X / Y. La collection est triée par « couches », donc je peux composite les images dans une sorte de l'algorithme de peintre.

De là, je peux déterminer le rectangle nécessaire pour contenir toutes les images, maintenant ce que je veux faire est:

  • Créer une sorte de tampon pour maintenir le résultat (l'équivalent de ce que NS appelle iPhoneOS UIGraphicsContext.)
  • Dessinez toutes les images dans la mémoire tampon.
  • Snag une nouvelle NSImage sur le résultat composited du tampon.

Dans iPhoneOS, c'est le code qui fait ce que je veux:

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();

Ce que je suis à la recherche est de savoir comment le faire dans le Bureau Cocoa / NS.

Merci!

Était-ce utile?

La solution

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];

Vérifier Guide Dessin d'Apple pour beaucoup plus longtemps, réponse plus détaillée.

Autres conseils

#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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top