2015-07-13 79 views
3

在我的應用程序中,我希望用戶能夠拍照,顯示照片,並通過點擊照片可以添加文本框以便他們可以在圖像上寫字。這與在Snapchat中向圖片添加文字的功能完全相同。Swift:直接從攝像頭看到視圖控制器/ UIImagePickerController

至於我能理解,只有這樣,才能在圖像已採取它,並能夠對其進行編輯後提出的,是設置:

 imagePicker.showsCameraControls = false 

創建自定義疊加:

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

    let overlayView = UIView(frame: CGRectMake(0, self.view.frame.width, self.view.frame.width, self.view.frame.height-self.view.frame.width)) 
    overlayView.backgroundColor = UIColor.blackColor() 
    overlayView.alpha = 0.5 
    println(overlayView) 

    let snapButton = UIView(frame: CGRectMake(150, 160, 80, 80)) 
    snapButton.layer.cornerRadius = 40 
    snapButton.userInteractionEnabled = true 
    snapButton.backgroundColor = UIColor.purpleColor() 
    overlayView.addSubview(snapButton) 
    overlayView.bringSubviewToFront(snapButton) 
    let recognizer = UITapGestureRecognizer(target: self, action:Selector("handleSnapTap:")) 
    recognizer.delegate = self 
    snapButton.addGestureRecognizer(recognizer) 

    let cancelButton = UIView(frame: CGRectMake(40, 40, 44, 44)) 
    cancelButton.layer.cornerRadius = 22 
    cancelButton.userInteractionEnabled = true 
    cancelButton.backgroundColor = UIColor.redColor() 
    overlayView.addSubview(cancelButton) 
    overlayView.bringSubviewToFront(cancelButton) 
    let cancelRecognizer = UITapGestureRecognizer(target: self, action:Selector("handleCancelTap:")) 
    cancelRecognizer.delegate = self 
    cancelButton.addGestureRecognizer(cancelRecognizer) 


    let changeCameraButton = UIView(frame: CGRectMake(165, 40, 44, 44)) 
    changeCameraButton.layer.cornerRadius = 22 
    changeCameraButton.userInteractionEnabled = true 
    changeCameraButton.backgroundColor = UIColor.blueColor() 
    overlayView.addSubview(changeCameraButton) 
    overlayView.bringSubviewToFront(changeCameraButton) 
    let changeCameraRecognizer = UITapGestureRecognizer(target: self, action:Selector("handleChangeCameraTap:")) 
    changeCameraRecognizer.delegate = self 
    changeCameraButton.addGestureRecognizer(changeCameraRecognizer) 

    let flashButton = UIView(frame: CGRectMake(300, 40, 44, 44)) 
    flashButton.layer.cornerRadius = 22 
    flashButton.userInteractionEnabled = true 
    flashButton.backgroundColor = UIColor.yellowColor() 
    overlayView.addSubview(flashButton) 
    overlayView.bringSubviewToFront(flashButton) 
    let flashRecognizer = UITapGestureRecognizer(target: self, action:Selector("handleFlashTap:")) 
    flashRecognizer.delegate = self 
    flashButton.addGestureRecognizer(flashRecognizer) 

    imagePicker.cameraOverlayView = overlayView 

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

func handleSnapTap(recognizer: UITapGestureRecognizer) { 
    println("Take picture") 
    imagePicker.takePicture() 
    self.performSegueWithIdentifier("cameraToImageViewSegue", sender: self) 

} 

func handleCancelTap(recognizer: UITapGestureRecognizer) { 
    println("Cancel") 
    self.imagePicker.dismissViewControllerAnimated(true, completion: nil) 
} 

func handleChangeCameraTap(recognizer: UITapGestureRecognizer) { 

    if (hasChangedCamera == nil){ 
    imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.Front 
     hasChangedCamera = true 
     return 
    } 

    if (hasChangedCamera == true){ 
     imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.Rear 
     hasChangedCamera = false 
     return 
    } 

    if (hasChangedCamera! == false){ 
     imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.Front 
     hasChangedCamera = true 
     return 

    } 
} 

func handleFlashTap(recognizer: UITapGestureRecognizer) { 

    if (hasTurnedOnFlash == nil){ 
     imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashMode.On 
     hasTurnedOnFlash = true 
     return 
    } 

    if (hasTurnedOnFlash == true){ 
     imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashMode.Off 
     hasTurnedOnFlash = false 
     return 
    } 

    if (hasTurnedOnFlash == false){ 
     imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashMode.On 
     hasTurnedOnFlash = true 
     return 
    } 

} 

最後呈現一個新的視圖控制器,其中所選擇的圖像被放置在UIView中,並從那裏進行編輯。我的問題是如何直接從UIImagePickerController繼承到新的視圖控制器。我曾嘗試以下:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { 

    self.imagePicker.dismissViewControllerAnimated(false, completion: nil) 
    let vc = ModifyCameraImageViewController() //change this to your class name 
    self.presentViewController(vc, animated: false, completion: nil) 

} 

首先,這只是導致黑屏,但我敢肯定有圍繞一個再簡單不過的方式。我的主要問題是,在下一個視圖控制器出現之前,在屏幕上出現UIImagePickerController所呈現的視圖控制器會短暫出現在屏幕上。這顯然不好看。我也嘗試刪除dismissViewController函數,以及將presentViewController函數放置在dismissView控制器函數的上方。這兩種嘗試給我的錯誤信息:

Warning: Attempt to present <xxx.ModifyCameraImageViewController: 0x145e3eb70> on <xxx.ViewController: 0x145d20a60> whose view is not in the window hierarchy! 

嘗試使用performSegueWithIdentifier與賽格瑞連接的基礎視圖和下一個視圖控制器給出了同樣的錯誤警告。

我發現以下類似的問題,但我的目標C完全不稱職,所以我努力使它的任何意義:Push a viewController from the UIImagePickerController camera view

所以,任何人都可以在關於如何呈現一個幫助視圖控制器直接從UIImagePickerController?

另外,請記住,我這樣做是爲了能夠在新挑選的圖像上創建文本疊加層(如Snapchat),所以如果任何人有更優雅的解決方案,請隨時發表它!

謝謝!

+0

這裏是一個很好的例子,回購完成這個任務:https://github.com/goktugyil/EZSwipeController – Esqarrouth

回答

5

好的,找到了我的問題的簡單解決方案。當按下takePicture按鈕並直接從那裏繼續到另一個視圖控制器時,不是從底層視圖控制器呈現imagePickerController,而是使用takePicture按鈕繼續到另一個視圖控制器,並從第二個視圖控制器的viewDidLoad呈現imagePickerController。第二個視圖控制器將在imagePickerController被解除時呈現。然而,這需要底層的視圖控制器看起來類似於相機控制,並且有些玩家使用動畫來看起來很自然。

0
let pickerController: Void = picker.dismissViewControllerAnimated(true) { _ in 
     UIImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage 
     self.performSegueWithIdentifier("segueIdentifier", sender: nil) 
     //Do what you want when picker is dismissed 
    } 
相關問題