在我的应用程序中,我希望用户能够拍照,显示图片,并通过点击照片,可以添加文本字段,以便他们可以在图像上书写。这与在Snapchat中为图片添加文本的功能完全相同。
据我所知,拍摄后可以编辑图像的唯一方法是设置:
def other_eligible_partners
rejecter_ids = Approval
.where(approved_id: id) # About ourselves
.where.not(rejected_at: nil) # Rejected us
.pluck(:user_id)
User.where.not(id: rejecter_ids + [id]) # People who didn't reject us
end
制作自定义叠加层:
imagePicker.showsCameraControls = false
最后呈现一个新的视图控制器,其中拾取的图像放在UIView中,并从那里编辑它。我的问题是如何直接从UIImagePickerController转换到新的视图控制器。我尝试过以下方法:
@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
}
}
首先,这只会导致黑屏,但我确信这方面有一个简单的方法。我的主要问题是,在下一个视图控制器出现之前,屏幕上会短暂显示UIImagePickerController所呈现的视图控制器。这显然不太好看。我还尝试删除dismissViewController函数,以及将presentViewController函数放在dismissView控制器函数之上。这两次尝试都给了我错误信息:
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)
}
尝试使用performSegueWithIdentifier和segue链接底层视图和下一个视图控制器会发出相同的错误警告。
我发现了以下类似的问题,但我对目标C完全不熟悉,所以我很难理解它:Push a viewController from the UIImagePickerController camera view
那么,任何人都可以帮助直接从UIImagePickerController呈现视图控制器吗?
此外,请记住,我这样做是为了能够在新挑选的图像上创建文本叠加层(如在Snapchat中),所以如果有人有更优雅的解决方案,请随意发布它!
谢谢!
答案 0 :(得分:5)
好的,找到了解决我问题的简单方法。当按下takePicture按钮时,不是从底层视图控制器呈现imagePickerController,而是直接从那里切换到另一个视图控制器,使用takePicture按钮切换到另一个视图控制器并从第二个视图控制器的viewDidLoad呈现imagePickerController。然后,当imagePickerController被解除时,将显示第二个视图控制器。然而,这需要底层视图控制器看起来类似于相机控件,并且一些玩动画为此看起来很自然。
答案 1 :(得分: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
}