Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - Mirroring back a picture taken from the front camera

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.

like image 239
Oliver Avatar asked Mar 31 '11 19:03

Oliver


1 Answers

I know this question was already answered, but the answer above didn't work for me so in case there are others...

UIImage *theImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    if (picker.cameraDevice == UIImagePickerControllerCameraDeviceFront) {
        CGSize imageSize = theImage.size;
        UIGraphicsBeginImageContextWithOptions(imageSize, YES, 1.0);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextRotateCTM(ctx, M_PI/2);
        CGContextTranslateCTM(ctx, 0, -imageSize.width);
        CGContextScaleCTM(ctx, imageSize.height/imageSize.width, imageSize.width/imageSize.height);
        CGContextDrawImage(ctx, CGRectMake(0.0, 0.0, imageSize.width, imageSize.height), theImage.CGImage);
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }

and newImage is your new image with the up orientation

like image 96
Andrew Park Avatar answered Sep 17 '22 16:09

Andrew Park