Adding UIView animation mysteriously changes starting frame
- by clozach
I'm working on an app that shows a thumbnail of a photo taken by the user. In order to show a seemless transition from the full-screen image shown in the UIImagePickerController to the thumbnail, I set the frame on my copy of the image to [[UIScreen mainScreen] bounds] and then change the frame to match the frame of an invisible button. (The button allows users to re-take their photo by tapping the thumbnail image.)
The trouble is that the starting x-origin of my UIImageView (imageView) is mysteriously starting nearly offscreen to the left:
As you can see from the alert, the imageView purports to be located at (0,0), yet it's displaying at something like (-255,0). Here's the code:
CGRect frame = [[UIScreen mainScreen] bounds];
imageView.frame = frame;
[[[[UIAlertView alloc] initWithTitle:@"Yo!" message:[NSString stringWithFormat:@"starting frame:%@",NSStringFromCGRect(imageView.frame)] delegate:nil cancelButtonTitle:@"K." otherButtonTitles:nil] autorelease] show];
// Slide the enlarged image seamlessly "onto" the photo button
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1000];
[UIView setAnimationDelay:.5];
imageView.frame = pictureButton.frame;
[UIView commitAnimations];
As if to taunt me, the image actually does go full screen if I comment out the animation code:
CGRect frame = [[UIScreen mainScreen] bounds];
imageView.frame = frame;
[[[[UIAlertView alloc] initWithTitle:@"Yo!" message:[NSString stringWithFormat:@"starting frame:%@",NSStringFromCGRect(imageView.frame)] delegate:nil cancelButtonTitle:@"K." otherButtonTitles:nil] autorelease] show];
// Slide the enlarged image seamlessly "onto" the photo button
// [UIView beginAnimations:nil context:nil];
// [UIView setAnimationDuration:1000];
// [UIView setAnimationDelay:.5];
//
// imageView.frame = pictureButton.frame;
//
// [UIView commitAnimations];
(Note: animationDuration will of course be set to something more like .5 in production, not 1000 seconds.)
Update — Other weirdness worth mentioning in case it's relevant:
It turns out the offset changes depending on the orientation of the camera when the photo was taken. If I take the picture with the phone upside-down, for instance, then the image gets offset vertically as well as horizontally.
The picker seems to be filling the image's imageOrientation incorrectly: I have to hold the phone sideways, Home button on the left, to get an image with UIImageOrientationUp.
At one point I experimented with setting imageView.clipsToBounds = YES;, which revealed that my image view's frame is in fact correct. Rather, it's the UIImage's placement within the UIImageView that gets offset.
Fwiw, the image view's contentMode is the default, UIViewContentModeScaleToFill.