2016-02-29 38 views
1

我正在嘗試自定義攝像頭視圖,基本上。我卡住更改UIImagePicker內部圖像視圖的大小。通過圖像視圖,我的意思是顯示相機視圖的窗口。我希望從圖中可以看得更清楚。它是那個紅色方塊內的視圖(我手動繪製了這個紅色方塊向你展示,不認爲它來自代碼)。自定義攝像頭視圖 - 無法更改cameraLayout UIImagePicker的幀大小,Swift

sample camera view

無論我試過至今不能幫我更改紅色矩形區域的大小。下面是我到目前爲止有:

@IBAction func takePhoto(sender: UIButton) { 
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){ 
     imagePicker = UIImagePickerController() 
     imagePicker.delegate = self 
     imagePicker.sourceType = .Camera 
     imagePicker.allowsEditing = false 
     imagePicker.showsCameraControls = false 

     //Create view 
     let customViewController = CustomCameraViewController(
      nibName:"CustomCameraViewController", 
      bundle: nil 
     ) 
     let customView:CustomCameraView = customViewController.view as! CustomCameraView 

     //Assignments 
     customView.frame = self.imagePicker.view.frame 
     customView.delegate = self 
     self.imagePicker.cameraOverlayView?.frame.size = customView.image.frame.size 

     presentViewController(imagePicker, 
      animated: true, 
      completion: { 
       self.imagePicker.cameraOverlayView = customView 
      } 
     ) 
    }else{ 
     //Alert 
     let alert = UIAlertController(title: "Üzgünüz, kameranızı açamadık!", message: "Telefonunuz kamerayı desteklemiyor veya açmamıza izin verilmemiş. İzin vermek isterseniz ayarlardan izni açabilirsiniz.", preferredStyle: UIAlertControllerStyle.Alert) 

     //Creating actions 
     let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default){ 
      UIAlertAction in 
      NSLog("OK is Pressed. Proceeding...") 
     } 

     //Adding actions 
     alert.addAction(okAction) 

     //Display alert 
     self.presentViewController(alert, animated: true, completion: nil) 
    } 
} 

我試圖從廈門國際銀行文件,完成整個自定義視圖的事情,它關係到CustomCameraView類。我爲CustomCameraView設置了兩個委託方法,可以使用這些方法,但這些對於此問題不是必需的,所以我不會發布代碼,但仍然認爲您可能需要知道。什麼是相關的問題是,廈門國際銀行文件,所以請在下面找到它:

CustomCameraViewController.xib file

的按鈕都出現在它們應該是,但是我不能讓使用該ImageView的組成。我在這裏創建了一個圖像視圖,以便我可以將其frame.size並設置爲選擇器的frame.size(您可以在我的代碼清單中看到這一點)。而且對於這個圖像視圖的引用是在CustomCameraView類中的,所以我正在從這個類中伸出,你也許會注意到。

我一直在嘗試一整天,但我無法找到一個解決方案,無論是在網上或我自己。我也讀過AVFoundation,但似乎沒有如何在UFoundation文檔中設置該視圖的幀大小的明確解釋,因爲我在UIImagePicker中決定問你們,因爲我在項目中迷失了方向,無法想象另一個現在的方式。

如果你能幫助我,我將非常感激。讓我知道你是否需要進一步的信息。

謝謝!

+0

您應該設置imagePicker的cameraViewTransform以獲得您想要的位置/比例。相機具有適用於不同模式(照片/視頻)的原生寬高比,因此可抵抗設置爲某些任意值。 – beyowulf

+0

感謝您的評論。你能給我一個使用cameraViewTransform的小例子嗎?我會很感激,如果你這樣做,請做回答,以便更容易看到。 –

回答

4

不知道你怎麼想改變cameraView的框架,但你能說類似下面的移動視圖100分的情況下,屏幕:

if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){ 
      imagePicker = UIImagePickerController() 
      imagePicker.delegate = self 
      imagePicker.sourceType = .Camera 
      imagePicker.allowsEditing = false 
      imagePicker.showsCameraControls = false 

      //Create view 
      let customViewController = CustomCameraViewController(
       nibName:"CustomCameraViewController", 
       bundle: nil 
      ) 
      let customView:CustomCameraView = customViewController.view as! CustomCameraView 

      //Assignments 
      customView.frame = self.imagePicker.view.frame 
      customView.delegate = self 
      imagePicker.cameraOverlayView = customView 
      //adjust the cameraView's position 
      imagePicker.cameraViewTransform = CGAffineTransformMakeTranslation(0.0, 100.0); 

      presentViewController(imagePicker, 
       animated: true, 
       completion: { 
       } 
      ) 
     } 

如果你想縮放cameraView到佔據更多的屏幕,你可以說CGAffineTransformMakeScale(scaleX,scaleY),但你將不得不裁剪產生的圖像,以匹配已經顯示給用戶。

+0

感謝您的時間。這個答案真的幫助我理解這個過程。所以我假設,我可能同時使用CGAffineTransformMakeTranslation和CGAffineTransformMakeScale。我確實嘗試過縮放,並縮小了尺寸。只是一個簡單的問題,如果你讓我。當我縮小尺寸時,實際上用戶看到的更少,但它以前所用的相同尺寸拍攝照片,是嗎? –

+0

是的。你只是縮放預覽而不是實際的圖像。 – beyowulf

+0

非常感謝! –

相關問題