Skip navigation.
 
mlRotating image problem
FROM : Markus Ruggiero
DATE : Mon Aug 28 12:00:07 2006

Hi list folks,

I am having a problem with images. Say I have an image from a digicam 
that is in portrait format. Finder preview show the image sideways in 
landscape. I open the image in Preview.app and rotate it. Save and 
close. Fine, image opens in Preview correctly. Also correct in 
GraphicConverter and other image tools. However Finder and my code 
still show the image sideways. What is the issue here?

Here is my code to display the image. This is in MyCustomView.h. The 
image data itself is obtained from a list of image path names

NSImage *theImage = [[NSImage alloc] initWithContentsOfFile:
[imagePathList objectAtIndex:index]];


- (void)drawRect:(NSRect)rect
{
   // expand the custom view to the size of the window
   NSRect frame = [[[self window] contentView] bounds];
   NSRect newFrame = NSMakeRect(NSMinX(frame), NSMinY(frame), NSWidth
(frame), NSHeight(frame));
   
   [self setFrame:newFrame];

   // fill the whole view with a nice background
   NSRect bounds = [self bounds];
   [[NSColor blackColor] set];
   NSRectFill(bounds);
   // if we have an image, draw it centered
   if (theImage != nil)
   {
       // find the pixel size of the image
       NSImageRep     *imageRep = [theImage bestRepresentationForDevice:nil];
       int pixelsHigh = [imageRep pixelsHigh];
       int pixelsWide = [imageRep pixelsWide];

       // we draw the iamge one pixel per one screen point
       // tis makes it 72 dpi (or whatever the physical dpi of the screen is)
       NSSize aSize = NSMakeSize(pixelsWide, pixelsHigh);
       [imageRep setSize:aSize];
       [theImage setSize:aSize];

       // handle scaling here        
       float vScale = NSHeight(bounds) / pixelsHigh;
       float hScale = NSWidth(bounds) / pixelsWide;

       // check if we have to downscale the image so that it fits into the 
window
       float  scaleFactor;

       if ( hScale < 1.0 || vScale < 1.0)
       {
           scaleFactor = vScale < hScale ? vScale : hScale;
       }
       else  // no downsizing needed
       {
           // check if we should upscale to maximum displayable size
           if (scaleUp)
               scaleFactor = vScale < hScale ? vScale : hScale;
           else
               scaleFactor = 1.0;
       }

       NSRect srcRect = NSMakeRect(0.0, 0.0, pixelsWide, pixelsHigh);

       NSRect dstRect = NSMakeRect(NSMidX(bounds) - (NSWidth(srcRect) * 
scaleFactor / 2),
                             NSMidY(bounds) - (NSHeight(srcRect) * scaleFactor / 2),
                             NSWidth(srcRect) * scaleFactor,
                             NSHeight(srcRect) * scaleFactor);
       
       [theImage drawInRect:dstRect fromRect:srcRect 
operation:NSCompositeSourceOver fraction:opacity];
   }
}

Thanks a lot
---markus---

http://blindpromo.com
My latest project, all done with WebObjects and Cocoa on Mac OS X

Related mailsAuthorDate
No related mails found.