IPhone – Mirroring back a picture taken from the front camera

cameraiphonemirroringuiimageuiimagepickercontroller

When using the front camera of the iPhone 4 to take a picture, the taken picture is mirrored compared with what you see on the iPhone screen. How may I restore the "on screen" view of the UIImage (not the UIImageView), and be able to save it like this ?

I tried :

UIImage* transformedImage = [UIImage imageWithCGImage:pickedImage.CGImage scale:1.0 orientation:UIImageOrientationLeftMirrored];
UIImageWriteToSavedPhotosAlbum (transformedImage, self, @selector(photoSaved:didFinishSavingWithError:contextInfo:), nil);

then putting it on screen. It is nearly the same as seen on screen, but the saved image is distorted.

So… How may I restore the "on screen" view of the UIImage (not the UIImageView), and be able to save it like this ?

I also tried this way :

UIImage* pickedImage = [[info objectForKey:UIImagePickerControllerOriginalImage] retain];
UIImage* transformedImage;

CGSize imageSize = pickedImage.size;
UIGraphicsBeginImageContextWithOptions(imageSize, YES, 1.0);
GContextRef ctx = UIGraphicsGetCurrentContext();

CGContextRotateCTM(ctx, 3.14);  // rotate by 180°
CGContextScaleCTM(ctx, 1.0, -1.0); // flip vertical
CGContextDrawImage(ctx, CGRectMake(0.0, 0.0, imageSize.width, imageSize.height), pickedImage.CGImage);
transformedImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

But that just gives a black image.

Best Answer

- (void)didTakePicture:(UIImage *)picture
{
    UIImage * flippedImage = [UIImage imageWithCGImage:picture.CGImage scale:picture.scale orientation:UIImageOrientationLeftMirrored];
    picture = flippedImage;
}