我尝试了很多在stackoverlfow上描述的答案,但是它们都没有提供我想要的结果。要么整个
UIImagePickerViewController
被转换,所以相机不直观,不像照镜子,要么只有图像后来被转换,如你所描述的那样,这样做虽然可以正常工作,但是你会在中间得到一个错误的预览图像。所以我最终采用了这种方法,它肯定不是最佳实践,但在iOS8和9上可以工作,并给我需要的结果。
确保添加一个观察者以在捕获图像按钮被按下时收到通知:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "photoTaken", name: "_UIImagePickerControllerUserDidCaptureItem", object: nil)
在
potoTaken
方法中,您可以像这样查找预览图像并将其转换为您所期望的外观:
func photoTaken() {
for subview in self.imagePicker.view.subviews {
for subsubview in subview.subviews {
for subsubsubview in subsubview.subviews {
for subsubsubsubview in subsubsubview.subviews {
for subsubsubsubsubview in subsubsubsubview.subviews {
for subsubsubsubsubsubview in subsubsubsubsubview.subviews {
for subsubsubsubsubsubsubview in subsubsubsubsubsubview.subviews {
if subsubsubsubsubsubsubview.isKindOfClass(UIImageView) {
subsubsubsubsubsubsubview.transform = CGAffineTransformScale(self.imagePicker.cameraViewTransform, -1, 1)
}
}
}
}
}
}
}
}
}
在你的 didFinishPickingMediaWithInfo
代理中,在保存或显示图像之前再次翻转图像。
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
let flippedImage = UIImage(CGImage: pickedImage.CGImage!, scale: pickedImage.scale, orientation: .LeftMirrored)
myCustomPhotoAlbum.sharedInstance.saveImage(flippedImage)
self.imageView.image = flippedImage
}
self.dismissViewControllerAnimated(true, completion: nil)
}