gpt4 book ai didi

ios - UIImagePickerController 相机 View 大小问题

转载 作者:技术小花猫 更新时间:2023-10-29 11:20:58 25 4
gpt4 key购买 nike

我正在使用 UIImagePickerController 和自定义叠加层构建一个应用程序。所做的是比较两个图像(前图像和后图像)。拍摄后照时,我使用了带有前照的自定义叠加层(请参阅所附图片)。

iPhone 5 - ios7 enter image description here

iPhone 4 - iOS7(拍照时) enter image description here

iPhone 4 - iOS 7(拍照后) enter image description here

查看 iPhone 4 和 iPhone 5 相机 View 之间的大小差异。

应用程序适用于 iPhone 5 屏幕尺寸(ios 6 和 ios7)。但是 iPhone 4/4s 屏幕尺寸,它只适用于 iOS6。问题出在 iphone 4/4s(仅限 ios7),相机 View 全屏显示。这意味着,你可以注意到iPhone 5 相机 View 尺寸 ~ 320*427 (iOS 6 和 iOS 7)iPhone 4 相机 View 尺寸 ~ 320*427 (iOS 6)但iPhone 4 相机 View 大小 ~ 320*480 (iOS 7)。

图片拍摄后,适合320*427的实际尺寸。由于这个问题,我无法在 iPhone 4 iOS7 上将前图像与相机 View 对齐(因为它的尖叫声为 320*480)。

有没有人遇到这个奇怪的问题。我几乎尝试了一切,但没有运气。有什么想法吗???

这是我的一段代码,用于在照片叠加之前使用自定义加载相机 View 。

- (void)loadCameraWithImage
{
if (!isLoadedOnce)
{
isLoadedOnce = YES;
UIImagePickerController *cameraView = [[UIImagePickerController alloc] init];
cameraView.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraView.wantsFullScreenLayout = NO;

if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
[self setEdgesForExtendedLayout:UIRectEdgeNone];
}


// crop before image

UIImage *imgTmpCropped = [self imageByCropping:imgBefore toRect:CGRectMake(0, 0, imgBefore.size.width/2, imgBefore.size.height)];
UIImage *overleyImage = [[UIImage alloc] initWithCGImage: imgTmpCropped.CGImage
scale: [UIScreen mainScreen].scale
orientation: UIImageOrientationDownMirrored];

UIImageView *crosshairView;
UIImageView *beforeView;

CGFloat screenHieght = [UIScreen mainScreen].bounds.size.height;

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
//overleyImage = [UIImage imageNamed:@"overlay_ipad.png"];
crosshairView = [[UIImageView alloc] initWithImage:overleyImage];
crosshairView.frame = CGRectMake(0, 0, 768, 1024);
[crosshairView setUserInteractionEnabled:YES];
crosshairView.backgroundColor = [UIColor clearColor];

beforeView = [[UIImageView alloc] initWithImage:overleyImage];
beforeView.frame = CGRectMake(0, 0, 384, 1024);
beforeView.alpha = 0.5;
beforeView.contentMode = UIViewContentModeScaleAspectFill;
}

else {
//overleyImage = [UIImage imageNamed:@"overleyImageAfter.png"];
crosshairView = [[UIImageView alloc] init];
beforeView = [[UIImageView alloc] initWithImage:overleyImage];

if(screenHieght>500){
crosshairView.frame = CGRectMake(0, 60, 320, 480);
beforeView.frame = CGRectMake(0, 70, 160, 427);
}
else{

if([[[UIDevice currentDevice] systemVersion] floatValue] <7.0){
crosshairView.frame = CGRectMake(0, 0, 320, 480);
beforeView.frame = CGRectMake(0, 0, 160, 427);
}
else{
crosshairView.frame = CGRectMake(0, 0, 320, 480);
beforeView.frame = CGRectMake(0, 0, 160, 480);
}

}

[crosshairView setUserInteractionEnabled:YES];
crosshairView.backgroundColor = [UIColor clearColor];
beforeView.alpha = 0.5;
beforeView.contentMode = UIViewContentModeScaleToFill;
}


//[crosshairView addSubview:beforeView];

//set our custom overlay view
cameraView.cameraOverlayView = beforeView;
cameraView.delegate = (id)self;
cameraView.showsCameraControls = YES;
cameraView.navigationBarHidden = YES;
cameraView.toolbarHidden = YES;
[cameraView setHidesBottomBarWhenPushed:YES];


[self.view.window.rootViewController presentViewController:cameraView animated:YES completion:nil];

isLoadedOnce = NO;
}

最佳答案

长话短说

相机的预览在每个设备上都有相同的宽高比 (4:3),屏幕则不同。假设预览将具有特定的纵横比,并将放置在屏幕上的特定位置。在该区域内绘制叠加层。

更长:

我们遇到了同样的问题。 (我们的用例是用与卡片大小相同的覆盖层拍摄信用卡照片)。主要问题是相机预览的比例在每台设备上始终相同 (4:3),但不同的手机有不同的屏幕比例(例如 iPhone 4s 与 iPhone 5),因此预览需要以不同的方式进行调整,这使得放置非常困难的叠加和裁剪。

我们的解决方案是(代码有点乱,不好意思,抱歉):

    // Adjust camera preview to be a little bit more centered instead of adjusted to the top
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
float cameraAspectRatio = 4.0 / 3.0;
float imageHeight = screenSize.width * cameraAspectRatio;
float verticalAdjustment;
if (screenSize.height - imageHeight <= 54.0f) {
verticalAdjustment = 0;
} else {
verticalAdjustment = (screenSize.height - imageHeight) / 2.0f;
verticalAdjustment /= 2.0f; // A little bit upper than centered
}
CGAffineTransform transform = self.cameraController.cameraViewTransform;
transform.ty += verticalAdjustment;
self.cameraController.cameraViewTransform = transform;

CGRect previewFrame = CGRectMake(0, verticalAdjustment, screenSize.width, imageHeight);
CardPhotoView *overlayView = [[CardPhotoView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, screenSize.height) widthPercentageOfCamera:self.widthPercentageOfCamera previewFrame:previewFrame];
self.overlayView = overlayView;

self.overlayView.delegate = self;
self.overlayView.useViewport = YES;
[self.overlayView setCameraReady:NO];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cameraIsReady:) name:AVCaptureSessionDidStartRunningNotification object:nil];

self.cameraController.showsCameraControls = NO;
self.cameraController.navigationBarHidden = YES;
self.cameraController.toolbarHidden = YES;
self.cameraController.cameraOverlayView = self.overlayView;

[self presentViewController:self.cameraController animated:NO completion:nil];

解释:

self.cameraController 是一个 UIImagePickerControllerCardPhotoView 是一个绘制叠加层的 View 子类,它需要预览框架才能准确知道预览的位置(有些设备的顶部和底部会有黑条,有些则没有)。它还需要一个宽度百分比来为裁剪窗口添加一点填充。此外,我们隐藏了所有默认的相机控件,并实现了一些可以为我们完成工作的按钮。这些按钮被添加到 CardPhotoView 类中,并且在绘制它们时会考虑预览框架的位置。

关于ios - UIImagePickerController 相机 View 大小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21700319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com