7

是否可以更改 UIImagePickerController 的可显示帧大小?我想显示相机视图,但不是在整个屏幕上,而是在 100x100 的边界框中显示。

这是我的 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];
 }

我在任何地方都找不到这样做的方法……没有人使用它吗?

4

3 回答 3

13

我对上一篇文章中的代码进行了试验,并注释掉了最终的比例变换((使其成为全尺寸的那个),最后我得到了一个可爱的微型相机 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];

我相信您可以对其进行更多自定义,更改相机视图的大小和位置。

于 2012-04-16T08:01:05.517 回答
3

如果你想在 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];

提示:在执行 cameraViewTransform 时,如果您希望用户最终获得与他们通过转换后的选择器视图看到的图像相同的图像,请确保在选择器为您捕获的结果照片上应用相同的转换:P

于 2013-03-06T01:59:11.967 回答
1

我有时会直接添加 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];

有兴趣看看你是否得到任何结果。

于 2012-04-15T18:23:09.367 回答