我正在使用 ZBar 来检测代码,但我还想启用从同一屏幕拍照的功能。我检测到横向拍照的奇怪行为。如果我将手机置于垂直横向位置,图像会正常显示,但如果我将 iPhone 移动到平面横向位置,则图像会上下颠倒。我检查了 UIImage 元数据并且图像方向具有不同的值,尽管在两种情况下设备方向是相同的。
知道为什么会这样吗?
我的解决方案是在错误的情况下更改图像方向元数据:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[....]
UIImage *image = [info objectForKey: UIImagePickerControllerOriginalImage];
if(image){
// This fixes a bug in ZBarReader taking picture in landscape orientation and device in flat position.
NSLog(@"Image: %d, Device: %d",image.imageOrientation,self.interfaceOrientation);
UIImageOrientation imgOrientation = image.imageOrientation;
UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft && imgOrientation == UIImageOrientationUp){
image = [UIImage imageWithCGImage:[image CGImage] scale:1.0 orientation:UIImageOrientationDown];
}else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight && imgOrientation == UIImageOrientationDown){
image = [UIImage imageWithCGImage:[image CGImage] scale:1.0 orientation:UIImageOrientationUp];
}
[self hideScanner];
[self performSegueWithIdentifier:@"mySegue" sender:image];
}
}
}
}