UIImagePickerController不填充屏幕
截图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的build议,我使用了这个代码:
// Resize the camera preview _imagePicker.cameraViewTransform = CGAffineTransformMakeScale(1.0, 1.03);
身高增加3%,工作得很好。 当我在屏幕底部添加自定义工具栏时,窗口上不再有可见的黑条。
相机的宽高比为4:3,屏幕的宽高比为3:2。 因此,除非您愿意裁剪3:2,否则相机图像根本无法填满屏幕。 为此,应用适当的缩放变换。
按固定值缩放并不是一个好主意……因为我确信任何使用这个接受答案的人都可能在iPhone 5出来的时候发现它。
这里有一个代码片段,可以根据屏幕分辨率进行dynamic缩放,以消除字母框。
// 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);
嘿,我看到一些人在计算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);
用这个转换它:
#define CAMERA_TRANSFORM 1.12412 _picker.wantsFullScreenLayout = YES; _picker.cameraViewTransform = CGAffineTransformScale(_picker.cameraViewTransform, CAMERA_TRANSFORM, CAMERA_TRANSFORM);
从我在iOS 7上的经验看,要将图片的中心看成全屏预览,您必须连接转换,而不是按顺序进行:
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);
这里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)
我用这个方法来计算比例。
// 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);
我的问题解决了:)
//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;
这在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)
尝试使用此代码:
picker.wantsFullScreenLayout = YES; CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 71.0); CGAffineTransform scale = CGAffineTransformScale(translate, 1.333333, 1.47); picker.view.transform = scale;
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]; } }