动态居中UIImagepicker取景器已放大到全屏

时间:2014-11-04 04:03:18

标签: ios objective-c iphone ios8 uiimagepickercontroller

我为iPhone 5制作全屏相机,并使用以下代码缩放4:3相机以填充整个屏幕,这是2:3的比例。左侧和右侧从屏幕上流下。

我必须将cameraView向下移动71个点才能使其与屏幕居中。否则,底部会出现黑条。我不太清楚为什么。因为我不知道为什么会发生这种情况,所以我无法弄清楚如何动态编码调整以适应iPhone 6和6 Plus。

感谢任何帮助。

// get the screen size
CGSize screenSize = [[UIScreen mainScreen] bounds].size;

// establish the height to width ratio of the camera
float heightRatio = 4.0f / 3.0f;

// calculate the height of the camera based on the screen width
float cameraHeight = screenSize.width * heightRatio;

// calculate the ratio that the camera height needs to be scaled by
float ratio = screenSize.height / cameraHeight;

//This slots the preview exactly in the middle of the screen by moving it down 71 points (for iphone 5)
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 71.0);
self.camera.cameraViewTransform = translate;

CGAffineTransform scale = CGAffineTransformScale(translate, ratio, ratio);
self.camera.cameraViewTransform = scale;

1 个答案:

答案 0 :(得分:2)

这终于点击了我的脑袋。因为我们知道相机的长度始终与屏幕宽度相同:

//以4:3的比例获取相机高度

int cameraViewHeight = SCREEN_WIDTH * 1.333;

int adjustedYPosition = (SCREEN_HEIGHT - cameraViewHeight) / 2;

CGAffineTransform translate = CGAffineTransformMakeTranslation(0, adjustedYPosition);
self.imagePicker.cameraViewTransform = translate;