views:

39

answers:

1

I have a problem with some simple code. The photo's, either picked or taken with the camera, are rotated. At first I thought it was a setting in the UIView but it happens when I copy the passed in UIImage to another UIImage using CGImageRef. I did it this way as it was the easiest way to ensure I was using a copy. Correct this code if I've screwed up please.

The code:

- (id)initWithImage:(UIImage *)image {
    if ((self = [super init]) && (image != nil)) {
        CGImageRef tmpImageRef = [image CGImage];
        puzzle = [[UIImage alloc] initWithCGImage:tmpImageRef];
    }

    return self;
}

The debugger:

    This GDB was configured as "--host=i386-apple-darwin --target=arm-apple-darwin".tty /dev/ttys001
Loading program into debugger…
sharedlibrary apply-load-rules all
Program loaded.
target remote-mobile /tmp/.XcodeGDBRemote-25694-49
Switching to remote-macosx protocol
mem 0x1000 0x3fffffff cache
mem 0x40000000 0xffffffff none
mem 0x00000000 0x0fff none
run
Running…
[Switching to thread 11523]
[Switching to thread 11523]
Re-enabling shared library breakpoint 1
Re-enabling shared library breakpoint 2
continue
2010-07-13 15:09:17.159 Golovomka[693:307] Using two-stage rotation animation. To use the smoother single-stage animation, this application must remove two-stage method implementations.
2010-07-13 15:09:17.172 Golovomka[693:307] Using two-stage rotation animation is not supported when rotating more than one view controller or view controllers not the window delegate
Current language:  auto; currently objective-c
(gdb) print (CGSize)[image size]
$1 = {
  width = 1536, 
  height = 2048
}
(gdb) n
28                  puzzle = [[UIImage alloc] initWithCGImage:tmpImageRef];
(gdb)
31          return self;
(gdb) print (CGSize)[puzzle size]
$2 = {
  width = 2048,
  height = 1536
}
(gdb)

The first print is on the CGIMageRef instantiation line. Any help gratefully received. As I said, this does not happen in the simulator and only when I deploy the code to a real device. Please note this post used to say that the problem only occurred debugging on devices and not in the simulator. I have since copied a photo taken with the camera on my iphone 3gs to the simulator and exactly the same problem occurs. So if you have a 2048x1536 pic lying around you should be able to duplicate this in the sim.

+1  A: 

UIImage's have an orientation property that you are ignoring when you extract the CGImage from the UIImage. You should be doing this:

if (self = [super init]) {
    puzzle = image; 
}
progrmr
That does preserve the orientation. I did just find this post http://stackoverflow.com/questions/1260249/resizing-uiimages-pulled-from-the-camera-also-rotates-the-uiimage covering a similar thing.When I did it that way before the code crashed and I assumed it was because image was being release later on. That doesn't appear to be the case now so I must've screwed up something else then when I googled for a solution I eneded up with the imageref method; which I thought was a bit crap for a modern language. Thank you!
Diziet
As a further note a lot later on in development I did end up translating and rotating the image as appropriate to remove this issue as it became a pain to deal with the orientation data in different libraries and systems. e.g. core graphics, opencv, bit manipulation.
Diziet