gpt4 book ai didi

ios - Swift:从相机/UIImagePickerController 直接转到 View Controller

转载 作者:搜寻专家 更新时间:2023-10-31 22:15:14 25 4
gpt4 key购买 nike

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

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

       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
}

}

最后展示一个新的 View Controller ,其中将选取的图像放置在 UIView 中,并从那里进行编辑。我的问题是如何直接从 UIImagePickerController 转到新的 View Controller 。我尝试了以下方法:

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)

}

首先,这只会导致黑屏,但我相信有一个足够简单的方法来解决这个问题。我的主要问题是,在下一个 View Controller 出现之前,显示 UIImagePickerController 的 View Controller 会短暂出现在屏幕上。这显然不好看。我还尝试删除 dismissViewController 函数,以及将 presentViewController 函数放在 dismissView Controller 函数之上。这两种尝试都给了我错误信息:

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

尝试将 performSegueWithIdentifier 与连接底层 View 和下一个 View Controller 的 segue 一起使用会给出相同的错误警告。

我发现了以下类似的问题,但我对 Objective C 完全无能,所以我很难理解它:Push a viewController from the UIImagePickerController camera view

那么,关于如何直接从 UIImagePickerController 呈现 View Controller ,任何人都可以提供帮助吗?

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

谢谢!

最佳答案

好的,找到了解决我的问题的简单方法。当按下 takePicture 按钮时,不是从底层 View Controller 呈现 imagePickerController,而是直接从那里转到另一个 View Controller ,而是使用 takePicture 按钮转到另一个 View Controller ,并从第二个 View Controller 的 viewDidLoad 呈现 imagePickerController。当 imagePickerController 被关闭时,第二个 View Controller 将被呈现。然而,这需要底层 View Controller 看起来与相机控件相似,并且需要一些动画来使它看起来自然。

关于ios - Swift:从相机/UIImagePickerController 直接转到 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31374076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com