使用iPhone 4的前置摄像头拍摄照片时,拍摄的照片与iPhone屏幕上显示的照片相比较。如何恢复UIImage(不是UIImageView)的“屏幕”视图,并能够像这样保存它?
我试过了:
UIImage* transformedImage = [UIImage imageWithCGImage:pickedImage.CGImage scale:1.0 orientation:UIImageOrientationLeftMirrored];
UIImageWriteToSavedPhotosAlbum (transformedImage, self, @selector(photoSaved:didFinishSavingWithError:contextInfo:), nil);
然后把它放在屏幕上。它与屏幕上显示的几乎相同,但保存的图像失真。
所以...我怎样才能恢复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();
但这只是一个黑色的图像。
答案 0 :(得分:27)
- (void)didTakePicture:(UIImage *)picture
{
UIImage * flippedImage = [UIImage imageWithCGImage:picture.CGImage scale:picture.scale orientation:UIImageOrientationLeftMirrored];
picture = flippedImage;
}
答案 1 :(得分:9)
我知道这个问题已经回答了,但上面的答案对我不起作用,以防有其他人......
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();
}
和newImage是具有向上方向的新图像
答案 2 :(得分:6)
最好的方法是将图像绘制到新的上下文中。
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);
这是内存写的,没有编译器的帮助,请不要复制和粘贴......
答案 3 :(得分:5)
安德鲁·帕克的回答很有效。这是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
}
答案 4 :(得分:4)
您可能希望镜像从相机获得的图像,但您最初得到的图像是正确的。用文字拍照进行比较。
答案 5 :(得分:4)
为Swift 3更新了一个稍微简单的答案:
var statusNames = [];
var hours = [];
var example = [{
"logged": {
"status": 0,
"status_name": "Logged",
"hours": 23 // to the hours array
},
"pending": {
"status": 3,
"status_name": "Pending",
"contribution": {
"hours": 0,
"count": 0
},
"verification": {
"hours": 6, // to the hours array
"count": 1
}
},
"denied": {
"status": 4,
"status_name": "Denied",
"contribution": {
"hours": 0,
"count": 0
},
"verification": {
"hours": 0, // to the hours array
"count": 0
}
},
"approved": {
"status": 2,
"status_name": "Approved",
"contribution": {
"hours": 4.5,
"count": 1
},
"verification": {
"hours": 0, // to the hours array
"count": 0
}
}
}];
for (var key in example[0]) {
statusNames.push(key);
hours.push(example[0][key]["hours"] || example[0][key]["verification"]["hours"] );
}
console.log(statusNames);
console.log(hours);
答案 6 :(得分:3)
正如其他答案,我遇到了同样的问题。但只是翻转最终图像只是答案的一半,因为UIPickerController在你用前置摄像头拍照时显示的预览图像,它仍然是倒置的(镜像)。
基于互联网的一些代码,我创建了一个Pod来获得这种想要的行为:
https://github.com/lucasecf/LEMirroredImagePicker
安装完成后,您只需要将这两行代码与UIImagePickerController
:
self.mirrorFrontPicker = [[LEMirroredImagePicker alloc] initWithImagePicker:pickerController];
[self.mirrorFrontPicker mirrorFrontCamera];
就是这样,就像那样。您可以在github链接的README中查看更多信息。
答案 7 :(得分:0)
我想投票给@Lucas Eduardo,我试图将组件LEImagePickerController集成到我的项目中,如下所示:
- (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];
}