Swift:直接从camera/UIImagePickerController选择到视图控制器 [英] Swift: Segue directly to a view controller from camera/UIImagePickerController

查看:116
本文介绍了Swift:直接从camera/UIImagePickerController选择到视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我希望用户能够拍照,并随图片一起展示,并通过点击照片可以添加文本字段,以便他们可以在图像上方书写.这与在Snapchat中向图片添加文本的功能完全相同.

In my app, I want the user to be able to be able to take a picture, be presented with the picture, and by tapping the photo a textfield can be added so that they can write on top of the image. This is exactly the same as the functionality of adding text to pictures in Snapchat.

据我所知,拍摄并能够编辑图像后呈现图像的唯一方法是设置:

As far as I can understand, the only way to be presented the image after having taken it and be able to edit it, is to set:

       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直接选择到新的视图控制器.我尝试了以下方法:

And finally present a new view controller in which the picked image is placed in a UIView, and edit it from there. My issue is how to segue directly from the UIImagePickerController to a new view controller. I have tried the following:

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控制器函数上方.这些尝试都给了我错误信息:

First off, this just leads to a black screen, but I'm sure there's a simple enough way around that. My main issue is the fact that the view controller from which the UIImagePickerController was presented briefly appears on the screen before the next view controller appears. This obviously does not look good. I also tried removing the dismissViewController function, as well as placing the presentViewController function above the dismissView controller function. Both of these attempts gave me the error message:

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

尝试将performSegueWithIdentifier与链接到基础视图和下一个视图控制器的segue一起使用时,会给出相同的错误警告.

Attempting to use performSegueWithIdentifier with a segue linking the underlying view and the next view controller gives the same error warning.

我发现了以下类似问题,但是我对Objective C完全无能为力,所以我很难理解这个问题:

I have found the following similar question, but I am completely inept at Objective C, so I'm struggling to make any sense of it: Push a viewController from the UIImagePickerController camera view

那么,任何人都可以在如何直接从UIImagePickerController呈现视图控制器方面提供帮助吗?

So, can anyone help in regards to how to present a view controller directly from the UIImagePickerController?

此外,请记住,我这样做是为了能够在新选择的图像上创建文本叠加层(例如在Snapchat中),因此,如果有人对此有更优雅的解决方案,请随时发布它!

Also, keep in mind that I'm doing this in order to be able to create a text overlay on the newly picked image (like in Snapchat), so if anyone has a more elegant solution to that, feel free to post it!

谢谢!

推荐答案

好,找到了解决我问题的简单方法.当按下takePicture按钮时,而不是从基础视图控制器中显示imagePickerController,然后直接从那里选择到另一个视图控制器,请使用takePicture按钮将其选择到另一个视图控制器,并从第二个视图控制器的viewDidLoad中显示imagePickerController.当关闭imagePickerController时,将显示第二个视图控制器.但是,这要求底层的视图控制器看起来与摄影机控件相似,并且需要一些动画来使之看起来自然.

Ok, found a simple solution to my issue. Instead of presenting the imagePickerController from the underlying view controller when the takePicture button is pressed, and segueing to another view controller directly from there, use the takePicture button to segue to another view controller and present the imagePickerController from the viewDidLoad of the second view controller. The second view controller will then be presented when the imagePickerController is dismissed. This however requires the underlying view controller to look similar to the camera controls, and some playing around with animations for this to look natural.

这篇关于Swift:直接从camera/UIImagePickerController选择到视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆