[英]Image moves down after capturing using ImagePicker Controller in iOS8
我在相机视图上放置了一个叠加图像并尝试捕获图像但是一旦我捕获图像(我还没有点击“使用照片”按钮),捕获的图像会向下移动一些像素并且顶部黑色的高度闪光灯和前/后相机设置按钮突然增加。 所以现在的情况是 - 我的叠加图像没有移动单个像素,并且捕获的图像向下移动。 因此,我无法在正确放置叠加层的情况下捕获正确的图像。 此问题仅适用于iOS8及更高版本。
在iOS8上遇到这个问题的人?
事实证明,对于iPhone(当UImagePickerController在导航控制器下显示时),当您拍照时,导航栏会被隐藏。 但是,在以下预览中,即使导航栏不可见,也不再隐藏导航栏,并且取消隐藏会根据方向向下或向右移动预览图像。 要补偿此更改,您必须相应地调整相机叠加视图的原点。 我通过添加一个处理UIImagePickerController通知的回调来解决这个问题。 在您启动UIImagePickerController的代码中添加以下代码:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleImagePickerNotification:)
name:@"_UIImagePickerControllerUserDidCaptureItem"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleImagePickerNotification:)
name:@"_UIImagePickerControllerUserDidRejectItem"
object:nil];
处理通知的回调必须区分UserDidCaptureItem和UserDidRejectItem通知。 在第一种情况下,相机覆盖帧的x或y原点递增(取决于方向),在第二种情况下,再次恢复增量。 增量或减量的大小是导航栏的高度。
- (void)handleImagePickerNotification:(NSNotification *)notification {
// This notification compensates for a feature (?) introduced with iOS 8 that moves the
// image after it's been taken with the UIImagePickerController.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGRect frame = cameraOverlay.frame;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGFloat barHeight = [self.navigationController navigationBar].frame.size.height;
if ([[notification name] isEqualToString:@"_UIImagePickerControllerUserDidCaptureItem"]) {
if (UIInterfaceOrientationIsLandscape(orientation))
frame.origin.x += barHeight;
else
frame.origin.y += barHeight;
} else
if ([[notification name] isEqualToString:@"_UIImagePickerControllerUserDidRejectItem"]) {
if (UIInterfaceOrientationIsLandscape(orientation))
frame.origin.x -= barHeight;
else
frame.origin.y -= barHeight;
[cameraOverlay setFrame:frame];
}
[cameraOverlay setFrame:frame];
}
这不是解决问题的一种非常优雅的方式,但在我的情况下它起作用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.