ios 配置 UIImagePickerController 的帧大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10164581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
configuring frame size of UIImagePickerController
提问by TommyG
Is it possible to change the displayable frame size of UIImagePickerController? I want to display camera view but not on the entire screen, but say in a 100x100 bounding box.
是否可以更改 UIImagePickerController 的可显示帧大小?我想显示相机视图但不是在整个屏幕上,而是在 100x100 的边界框中说。
Here is my viewDidAppear:
这是我的 viewDidAppear:
- (void) viewDidAppear:(BOOL)animated {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.showsCameraControls = NO;
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
picker.cameraViewTransform = CGAffineTransformScale(picker.cameraViewTransform, CAMERA_TRANSFORM_X, CAMERA_TRANSFORM_Y);
[self presentModalViewController:picker animated:YES];
[super viewDidAppear:YES];
}
I could not find a way to do that anywhere...doesnt anyone using it?
我在任何地方都找不到这样做的方法……没有人使用它吗?
回答by SomaMan
I experimented with the code from my last post, and commented out the final scale transform ((the one which makes it full size) and I ended up with a lovely miniature camera imagePicker floating in the middle of my screen, so it definitely does work! The exact code I used, including the zoom/fade-in transition, is -
我尝试了上一篇文章中的代码,并注释掉了最终的缩放变换((使它成为全尺寸的那个),最后我得到了一个可爱的微型相机 imagePicker 漂浮在我的屏幕中间,所以它确实有效!我使用的确切代码,包括缩放/淡入过渡,是 -
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
UIView *controllerView = imagePickerController.view;
controllerView.alpha = 0.0;
controllerView.transform = CGAffineTransformMakeScale(0.5, 0.5);
[[[[UIApplication sharedApplication] delegate] window] addSubview:controllerView];
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
controllerView.alpha = 1.0;
}
completion:nil
];
[imagePickerController release];
I'm sure you could customise it more, change the size & location of the camera view.
我相信您可以对其进行更多自定义,更改相机视图的大小和位置。
回答by Alex the Ukrainian
If you want to display an overlay on top of the UIImagePickerController, such as in my case, while at the same time fitting the controller's "live camera feed" (the view) in between some UI components on your overlay (such as a top bar and a bottom bar), use the following code. This is based on SomaMan's answer above (THANKS), with the main difference of displaying UIImagePickerController's view as a subview of the current controller instead of as a subview of the main application window. Put this code in viewDidLoad():
如果你想在 UIImagePickerController 的顶部显示一个叠加层,比如在我的例子中,同时在叠加层上的一些 UI 组件之间安装控制器的“实时相机馈送”(视图)(例如顶部栏)和底部栏),使用以下代码。这是基于 SomaMan 上面的回答(谢谢),主要区别在于将 UIImagePickerController 的视图显示为当前控制器的子视图而不是主应用程序窗口的子视图。将此代码放在 viewDidLoad() 中:
// Overlay view with custom camera controls and a top bar and a bottom bar
self.overlay = [[CameraOverlayView alloc] initWithFrame:self.view.bounds];
[self setOverlayViewModel];
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
self.imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
self.imagePicker.showsCameraControls = NO;
self.imagePicker.navigationBarHidden = YES;
self.imagePicker.toolbarHidden = YES;
self.imagePicker.delegate = self;
UIView *imagePickerView = self.imagePicker.view;
if ([[UIScreen mainScreen] bounds].size.height == 568.0f) {
// iPhone 5, 16:9 ratio, need to "zoom in" in order to fill the screen since there is extra space between top and bottom bars on a taller screen
self.imagePicker.cameraViewTransform = CGAffineTransformScale(self.imagePicker.cameraViewTransform, 1.5, 1.5); // change 1.5 to suit your needs
}
CGRect cameraViewFrame = CGRectMake(0, self.overlay.topBarHeight,
self.view.bounds.size.width,
self.view.bounds.size.height - self.overlay.topBarHeight - self.overlay.bottomBarHeight);
imagePickerView.frame = cameraViewFrame;
// keep this order so that the overlay view is on top of the "live camera feed" view
[self.view addSubview:imagePickerView];
[self.view addSubview:self.overlay];
TIP: When doing cameraViewTransform, make sure to apply the same transform on the resulting photo that the picker captures for you if you want the user to end up with the same image that they see through your transformed picker's view :P
提示:在执行 cameraViewTransform 时,如果您希望用户最终获得与他们通过转换后的选择器视图看到的相同的图像,请确保对选择器为您捕获的结果照片应用相同的变换:P
回答by SomaMan
I've sometimes added the ImagePicker's view directly, though I've never experimented with changing its final size, it does "zoom" into the screen, suggesting that it might be possible to show it at different sizes (code lifted directly from one of my projects, so probably not all relevant) -
我有时会直接添加 ImagePicker 的视图,尽管我从未尝试过更改其最终大小,但它确实“放大”了屏幕,这表明可能可以以不同的大小显示它(代码直接从其中之一中提取)我的项目,所以可能不是所有相关的)-
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
UIView *controllerView = imagePickerController.view;
controllerView.alpha = 0.0;
controllerView.transform = CGAffineTransformMakeScale(0.5, 0.5);
[[[[UIApplication sharedApplication] delegate] window] addSubview:controllerView];
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
controllerView.transform = CGAffineTransformMakeScale(1.0, 1.0);
controllerView.alpha = 1.0;
}
completion:nil
];
[imagePickerController release];
Be interested to see if you get any results with this.
有兴趣看看你是否得到任何结果。