2012-04-15 62 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