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 scre
Andrew Park's answer works great. This is Swift version.
func flipImage(image: UIImage!) -> UIImage! {
let imageSize:CGSize = image.size;
UIGraphicsBeginImageContextWithOptions(imageSize, true, 1.0);
let ctx:CGContextRef = UIGraphicsGetCurrentContext()!;
CGContextRotateCTM(ctx, CGFloat(M_PI/2.0));
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), image.CGImage);
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage
}
- (void)didTakePicture:(UIImage *)picture
{
UIImage * flippedImage = [UIImage imageWithCGImage:picture.CGImage scale:picture.scale orientation:UIImageOrientationLeftMirrored];
picture = flippedImage;
}
The best way is to draw the image into a new context.
CGSize imageSize = pickedImage.size;
UIGraphicsBeginImageContextWithOptions(imageSize, YES, 1.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, imageSize.width, 0.0);
CGContextScaleCTM(ctx, -1.0, 1.0);
CGContextDrawImage(ctx, pickedImage.CGImage, CGRectMake(0.0, 0.0, imageSize.width, imageSize.height));
UIImage *transformedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum (transformedImage, self, @selector(photoSaved:didFinishSavingWithError:contextInfo:), nil);
This was written out of memory and without aid of a compiler, please don't copy and paste...
You might want to mirror the image you get from the camera but what you originally get is correct. Take a picture with text to compare.
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
A slightly simpler answer updated for Swift 3:
func flipImage(image: UIImage) -> UIImage {
guard let cgImage = image.cgImage else {
// Could not form CGImage from UIImage for some reason.
// Return unflipped image
return image
}
let flippedImage = UIImage(cgImage: cgImage,
scale: image.scale,
orientation: .leftMirrored)
return flippedImage
}