可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
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...
回答2:
- (void)didTakePicture:(UIImage *)picture
{
UIImage * flippedImage = [UIImage imageWithCGImage:picture.CGImage scale:picture.scale orientation:UIImageOrientationLeftMirrored];
picture = flippedImage;
}
回答3:
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
回答4:
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
}
回答5:
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.
回答6:
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
}
回答7:
As the other answers, I had the same problem. But just flip the final image is only half the answer, because the preview image, displayed by the UIPickerController when you take a picture with the front camera, it's still inverted (mirrored).
Based in some codes from internet, I created a Pod to get this wanted behavior:
https://github.com/lucasecf/LEMirroredImagePicker
After installed, you just have to call this two lines of code together with your UIImagePickerController
:
self.mirrorFrontPicker = [[LEMirroredImagePicker alloc] initWithImagePicker:pickerController];
[self.mirrorFrontPicker mirrorFrontCamera];
And thats it, simply as that. You can check for more informations in the README of the github link.
回答8:
I'd like to vote up @Lucas Eduardo , I have tried to integrate the component LEImagePickerController into my project as below:
- (void)onCamera:(id)sender {
UIImagePickerController* imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
imagePicker.delegate = self;
//cover the switch button at the top right corner, I just need user take photo with the front facing camera
UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
view.center = CGPointMake(CGRectGetWidth(imagePicker.view.bounds) - CGRectGetWidth(view.bounds)/2, CGRectGetHeight(view.bounds)/2);
view.backgroundColor = [UIColor blackColor];
[imagePicker.view addSubview:view];
LEMirroredImagePicker* mirrorFrontPicker = [[LEMirroredImagePicker alloc] initWithImagePicker:imagePicker];
[mirrorFrontPicker mirrorFrontCamera];
[self presentViewController:imagePicker animated:YES completion:nil];
}