views:

138

answers:

2

I've got a program that can open TIFF documents and display them. I'm using setFlipped:YES.

If I'm just dealing with single page image files, I can do

[image setFlipped: YES];

and that, in addition to the view being flipped, seems to draw the image correctly.

However, for some reason, setting the flipped of the image doesn't seem to affect the flippedness of the individual representations.

This is relevant because the multiple images of a multi-page TIFF seem to appear as different "representations" of the same image. So, if I just draw the IMAGE, it's flipped, but if I draw a specific representation, it isn't flipped. I also can't seem to figure out how to chose which representation is the default one that gets drawn when you draw the NSImage.

thanks.

A: 

I believe that the answer is that Yes, different pages are separate representations, and the correct way to deal with them is to turn them into images with:

NSImage *im = [[NSImage alloc] initWithData:[representation TIFFRepresentation]];
[im setFlipped:YES];
Brian Postow
+1  A: 

You shouldn't use the -setFlipped: method to control how the image is drawn. You should use a transform based on the flipped-ness of the context you are drawing into. Something like this (a category on NSImage):

@implementation NSImage (FlippedDrawing)
- (void)drawAdjustedInRect:(NSRect)dstRect fromRect:(NSRect)srcRect operation:(NSCompositingOperation)op fraction:(CGFloat)delta
{
    NSGraphicsContext* context = [NSGraphicsContext currentContext];
    BOOL contextIsFlipped      = [context isFlipped];

    if (contextIsFlipped)
    {
     NSAffineTransform* transform;

     [context saveGraphicsState];

     // Flip the coordinate system back.
     transform = [NSAffineTransform transform];
     [transform translateXBy:0 yBy:NSMaxY(dstRect)];
     [transform scaleXBy:1 yBy:-1];
     [transform concat];

     // The transform above places the y-origin right where the image should be drawn.
     dstRect.origin.y = 0.0;
    }

    [self drawInRect:dstRect fromRect:srcRect operation:op fraction:delta];

    if (contextIsFlipped)
    {
     [context restoreGraphicsState];
    }

}
- (void)drawAdjustedAtPoint:(NSPoint)point
{
    [self drawAdjustedAtPoint:point fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
}

- (void)drawAdjustedInRect:(NSRect)rect
{
    [self drawAdjustedInRect:rect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
}

- (void)drawAdjustedAtPoint:(NSPoint)aPoint fromRect:(NSRect)srcRect operation:(NSCompositingOperation)op fraction:(CGFloat)delta
{
    NSSize size = [self size];
    [self drawAdjustedInRect:NSMakeRect(aPoint.x, aPoint.y, size.width, size.height) fromRect:srcRect operation:op fraction:delta];
}
@end
Rob Keniger
That was the first technique I tried, but for some reason, the transforms only got executed the first time I drew the image, so when I re-sized the window, the image became upside down...
Brian Postow