2012-04-15 61 views
6

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

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

我无法找到一个方法来做到这一点的任何地方......用它does not任何人吗?

+0

您是否解决了您的问题? – 2013-05-16 12:40:04

回答

12

我用我上一篇文章的代码进行了实验,并将最终的缩放转换((使其成为完整大小的转换)进行了注释,最后我在屏幕中间浮动了一个可爱的微型相机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]; 

我敢肯定,你可以更定制,改变相机视图的大小&位置

+1

awsome工作伙计 – coder1010 2013-05-03 10:24:42

+1

您的代码工作良好。但是覆盖屏幕的底部边距超过了顶部边距,因此它不会完全进入屏幕的中心。任何解决方案? – 2013-05-08 13:34:47

+0

只需在动画中更改controllerView的最终位置:块 – SomaMan 2013-05-24 10:49:15

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]; 

有兴趣看看你得到任何与此结果。

+0

nop,没有工作,对不起......我很惊讶,那么简单的东西无法找到任何地方。 – TommyG 2012-04-15 21:47:46

3

如果你想在UIImagePickerController之上显示一个叠加层,例如在我的情况下,而在相同的实时将控制器的“实时摄像头馈送”(视图)放置在覆盖图上的某些UI组件(如顶栏和底栏)之间,请使用以下代码。这是基于SomaMan的上面的回答(THANKS),主要区别在于将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

+0

@ Alex - 如何在uiimage上设置变换? – 2013-10-19 06:11:09

+0

@ kshitijgodara嗨,对不起,我刚才注意到了这一点,但基本上我放弃了使用UIImagePickerController由于其局限性。使用AV框架并不难,我写了一篇博客文章,将其作为其他一些东西的一部分:http://radioactiveukrainian.com/2014/01/06/testing-a-camera-app -in-IOS-模拟器/ – 2014-04-01 22:10:56