screenshot http://i39.tinypic.com/oiermb.png
我正在为UIImagePickerController添加一个自定义叠加层,并且视图底部有一个持久的黑条。这是我实例化控制器的代码。
- (UIImagePickerController *)imagePicker {
if (_imagePicker) {
return _imagePicker;
}
_imagePicker = [[UIImagePickerController alloc] init];
_imagePicker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
_imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
_imagePicker.showsCameraControls = NO;
_imagePicker.wantsFullScreenLayout = YES;
_imagePicker.navigationBarHidden = YES;
_imagePicker.toolbarHidden = YES;
} else {
_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
return _imagePicker;
}
当我没有隐藏相机控件时,返回的控制器以模态显示并且工作正常(即显示全屏)。
感谢Ole的建议,我使用了这段代码:
// Resize the camera preview
_imagePicker.cameraViewTransform = CGAffineTransformMakeScale(1.0, 1.03);
高度增加3%就可以了。当我在屏幕底部添加自定义工具栏时,窗口上不再有可见的黑条。
答案 0 :(得分:51)
按固定价值进行缩放并不是一个好主意......因为我确信在iPhone 5出现时,任何使用此接受答案的人都可能会发现。
这是一个代码片段,可根据屏幕分辨率动态缩放以消除字母装箱。
// Device's screen size (ignoring rotation intentionally):
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
// iOS is going to calculate a size which constrains the 4:3 aspect ratio
// to the screen size. We're basically mimicking that here to determine
// what size the system will likely display the image at on screen.
// NOTE: screenSize.width may seem odd in this calculation - but, remember,
// the devices only take 4:3 images when they are oriented *sideways*.
float cameraAspectRatio = 4.0 / 3.0;
float imageWidth = floorf(screenSize.width * cameraAspectRatio);
float scale = ceilf((screenSize.height / imageWidth) * 10.0) / 10.0;
self.ipc.cameraViewTransform = CGAffineTransformMakeScale(scale, scale);
答案 1 :(得分:37)
相机的宽高比为4:3,屏幕的宽高比为3:2。所以除非你愿意裁剪到3:2,否则根本无法让相机图片填满屏幕。为此,请应用适当的比例变换。
答案 2 :(得分:20)
嘿,我看到有些人在计算iPhone 5的比例后仍然在底部的黑色条。我有一段时间有这个问题,但后来我发现你必须翻译视图所以它在中间屏幕然后应用比例。这是我做这两件事的代码,对我有用!
CGSize screenBounds = [UIScreen mainScreen].bounds.size;
CGFloat cameraAspectRatio = 4.0f/3.0f;
CGFloat camViewHeight = screenBounds.width * cameraAspectRatio;
CGFloat scale = screenBounds.height / camViewHeight;
m_imagePickerController.cameraViewTransform = CGAffineTransformMakeTranslation(0, (screenBounds.height - camViewHeight) / 2.0);
m_imagePickerController.cameraViewTransform = CGAffineTransformScale(m_imagePickerController.cameraViewTransform, scale, scale);
答案 3 :(得分:10)
用它转换它:
#define CAMERA_TRANSFORM 1.12412
_picker.wantsFullScreenLayout = YES;
_picker.cameraViewTransform = CGAffineTransformScale(_picker.cameraViewTransform, CAMERA_TRANSFORM, CAMERA_TRANSFORM);
答案 4 :(得分:10)
根据我在iOS 7,iphone 5S上的经验,要看到图片中心进入全屏预览,你必须连接转换,而不是按顺序进行转换:
pickerController.cameraOverlayView = self.view;
CGSize screenSize = [[UIScreen mainScreen] bounds].size; // 320 x 568
float scale = screenSize.height / screenSize.width*3/4; // screen height divided by the pickerController height ... or: 568 / ( 320*4/3 )
CGAffineTransform translate=CGAffineTransformMakeTranslation(0,(screenSize.height - screenSize.width*4/3)*0.5);
CGAffineTransform fullScreen=CGAffineTransformMakeScale(scale, scale);
pickerController.cameraViewTransform =CGAffineTransformConcat(fullScreen, translate);
答案 5 :(得分:4)
这里是Swift的答案。
let screenSize:CGSize = UIScreen.mainScreen().bounds.size
let ratio:CGFloat = 4.0 / 3.0
let cameraHeight:CGFloat = screenSize.width * ratio
let scale:CGFloat = screenSize.height / cameraHeight
let imag:UIImagePickerController = UIImagePickerController()
imag.cameraViewTransform = CGAffineTransformMakeTranslation(0, (screenSize.height - cameraHeight) / 2.0)
imag.cameraViewTransform = CGAffineTransformScale(imag.cameraViewTransform, scale, scale)
答案 6 :(得分:4)
这适用于iOS 10:
let screenSize = UIScreen.main.bounds.size
let cameraAspectRatio = CGFloat(4.0 / 3.0)
let cameraImageHeight = screenSize.width * cameraAspectRatio
let scale = screenSize.height / cameraImageHeight
imagePickerController.cameraViewTransform = CGAffineTransform(translationX: 0, y: (screenSize.height - cameraImageHeight)/2)
imagePickerController.cameraViewTransform = imagePickerController.cameraViewTransform.scaledBy(x: scale, y: scale)
答案 7 :(得分:3)
我用这种方法来计算比例。
// 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 scale = screenSize.height / cameraHeight;
imagePicker.cameraViewTransform = CGAffineTransformMakeScale(scale, scale);
答案 8 :(得分:2)
我的问题解决了:)
//Camera is 426 * 320. Screen height is 568. Multiply by 1.333 in 5 inch (iPhone5) to fill vertical
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 71.0); //This slots the preview exactly in the middle of the screen by moving it down 71 points
self.imagePickerController.cameraViewTransform = translate;
CGAffineTransform scale = CGAffineTransformScale(translate, 1.333333, 1.333333);
self.imagePickerController.cameraViewTransform = scale;
答案 9 :(得分:2)
这些解决方案对我来说根本不起作用。在iOS 13.x下运行并使用iPhone 6+和iPhone 11 Pro的底部仍然存在一个黑条。通过计算到中心的偏移量并将其应用于比例计算,我能够解决这两个问题,而无需使用幻数。 6+和11 Pro都可以再次使用。
let screenSize = UIScreen.main.bounds.size
let cameraAspectRatio = CGFloat(4.0 / 3.0)
let cameraImageHeight = screenSize.width * cameraAspectRatio
let offsetToCenter = screenSize.height - cameraImageHeight
let scale = (screenSize.height + offsetToCenter) / cameraImageHeight
if UIImagePickerController.isSourceTypeAvailable(.camera) {
myPickerController.delegate = self
myPickerController.sourceType = .camera
myPickerController.showsCameraControls = false
myPickerController.allowsEditing = false
myPickerController.cameraViewTransform = CGAffineTransform(scaleX: scale, y: scale)
myPickerController.cameraOverlayView = CameraOverlay(frame: vc.view.frame, delegate: self)
currentVC.present(myPickerController, animated: true)
}
}
答案 10 :(得分:0)
尝试使用此代码:
picker.wantsFullScreenLayout = YES;
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 71.0);
CGAffineTransform scale = CGAffineTransformScale(translate, 1.333333, 1.47);
picker.view.transform = scale;
答案 11 :(得分:0)
PickerViewController.m
#define CAMERA_TRANSFORM 1.8
- (void)viewDidAppear:(BOOL)animated
{
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
// type = UIImagePickerControllerSourceTypeCamera;
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = NO;
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.showsCameraControls=NO;
picker.extendedLayoutIncludesOpaqueBars = YES;
picker.cameraViewTransform = CGAffineTransformScale(picker.cameraViewTransform, CAMERA_TRANSFORM , CAMERA_TRANSFORM);
[self presentViewController:picker animated:YES completion:nil];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message"
message:@"Device have no camera"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
答案 12 :(得分:0)
这是 Swift 4
let screenSize: CGSize = picker.view.safeAreaLayoutGuide.layoutFrame.size
let ratio: CGFloat = 4.0 / 3.0
let cameraHeight: CGFloat = screenSize.width * ratio
let scale: CGFloat = (screenSize.height + 80) / cameraHeight
picker.cameraViewTransform = picker.cameraViewTransform.scaledBy(x: scale, y: scale)
答案 13 :(得分:0)
雨燕5
100%工作 简单的解决方案 如果要在某些自定义UIView中显示摄像机,请使用该代码和摄像机将以全屏显示
class ViewController: UIViewController, UINavigationControllerDelegate,UIImagePickerControllerDelegate {
}
上半部分是代表,下半部分是函数
//Mark:- Call this function and enjoy
var imagePickers:UIImagePickerController?
func addCameraInView(){
imagePickers = UIImagePickerController()
if UIImagePickerController.isCameraDeviceAvailable( UIImagePickerController.CameraDevice.rear) {
imagePickers?.delegate = self
imagePickers?.sourceType = UIImagePickerController.SourceType.camera
//imagePickers?.view.frame = customCameraView.bounds
imagePickers?.view.contentMode = .scaleAspectFit
imagePickers?.allowsEditing = false
imagePickers?.showsCameraControls = false
imagePickers?.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
//Mark:- this part is handling camera in full screen in a custom view
// let screenSize = UIScreen.main.bounds.size
let screenSize = customCameraView.bounds.size
let cameraAspectRatio = CGFloat(4.0 / 4.0)
let cameraImageHeight = screenSize.width * cameraAspectRatio
let scale = screenSize.height / cameraImageHeight
imagePickers?.cameraViewTransform = CGAffineTransform(translationX: 0, y: (screenSize.height - cameraImageHeight)/2)
imagePickers?.cameraViewTransform = (imagePickers?.cameraViewTransform.scaledBy(x: scale, y: scale))!
// Add the child's View as a subview
self.customCameraView.addSubview((imagePickers?.view)!)
}
}
如果要在主屏幕上使用它,则需要更改比例(4.0 / 3.0)
let screenSize = UIScreen.main.bounds.size
let cameraAspectRatio = CGFloat(4.0 / 3.0)