Oli*_*ver 10 iphone camera mirroring uiimagepickercontroller uiimage
使用iPhone 4的前置摄像头拍摄照片时,拍摄的照片与iPhone屏幕上显示的照片相比较.我怎样才能恢复UIImage(不是UIImageView)的"屏幕"视图,并能够像这样保存它?
我试过了 :
UIImage* transformedImage = [UIImage imageWithCGImage:pickedImage.CGImage scale:1.0 orientation:UIImageOrientationLeftMirrored];
UIImageWriteToSavedPhotosAlbum (transformedImage, self, @selector(photoSaved:didFinishSavingWithError:contextInfo:), nil);
Run Code Online (Sandbox Code Playgroud)
然后把它放在屏幕上.它与屏幕上显示的几乎相同,但保存的图像失真.
那么......我怎样才能恢复UIImage(不是UIImageView)的"屏幕"视图,并能够像这样保存它?
我也尝试过这种方式:
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();
Run Code Online (Sandbox Code Playgroud)
但这只是一个黑色的图像.
Moh*_*sul 27
- (void)didTakePicture:(UIImage *)picture
{
UIImage * flippedImage = [UIImage imageWithCGImage:picture.CGImage scale:picture.scale orientation:UIImageOrientationLeftMirrored];
picture = flippedImage;
}
Run Code Online (Sandbox Code Playgroud)
我知道这个问题已经回答了,但上面的答案对我不起作用,以防有其他人......
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();
}
Run Code Online (Sandbox Code Playgroud)
和newImage是你的新图像,具有向上的方向
最好的方法是将图像绘制到新的上下文中.
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);
Run Code Online (Sandbox Code Playgroud)
这是内存写的,没有编译器的帮助,请不要复制和粘贴...
安德鲁公园的答案很有效.这是Swift版本.
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
}
Run Code Online (Sandbox Code Playgroud)
为 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
}
Run Code Online (Sandbox Code Playgroud)