iPhone - Mirroring back a picture taken from the front camera

前端 未结 8 2011
半阙折子戏
半阙折子戏 2020-12-24 04:06

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

相关标签:
8条回答
  • 2020-12-24 04:11

    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
    }
    
    0 讨论(0)
  • 2020-12-24 04:21
    - (void)didTakePicture:(UIImage *)picture
    {
        UIImage * flippedImage = [UIImage imageWithCGImage:picture.CGImage scale:picture.scale orientation:UIImageOrientationLeftMirrored];
        picture = flippedImage;
    }
    
    0 讨论(0)
  • 2020-12-24 04:24

    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...

    0 讨论(0)
  • 2020-12-24 04:26

    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.

    0 讨论(0)
  • 2020-12-24 04:27

    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

    0 讨论(0)
  • 2020-12-24 04:27

    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
    }
    
    0 讨论(0)
提交回复
热议问题