为什么每当我加载UIImagePickerController视图时我的键盘都会被加载?

时间:2016-09-02 04:11:46

标签: ios swift uiimagepickercontroller

我有一个UIActionSheet用于在相机或照片库之间进行选择,以便将图像嵌入到UITextView中,但无论出于何种原因,它都会加载键盘。我按下UITextView周围栏的左侧按钮关闭键盘,但是当我按下照片库时,我会打开并关闭键盘,然后再按下图像选择器VC。

override func didPressLeftButton(sender: AnyObject?) {
    let cameraMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)

    let photoLibrary = UIAlertAction(title: "Photo Library", style: .Default, handler: { (UIAlertAction) in
        self.openPhotoLibrary()
    })

    let takePhoto = UIAlertAction(title: "Open Camera", style: .Default, handler: { (UIAlertAction) in
        self.textView.endEditing(true)
        self.openCamera()
    })

    let cancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)

    cameraMenu.addAction(photoLibrary)
    cameraMenu.addAction(takePhoto)
    cameraMenu.addAction(cancel)

    self.presentViewController(cameraMenu, animated: true, completion: nil)
}

func openPhotoLibrary() {
    imagePicker.sourceType = .PhotoLibrary
    imagePicker.allowsEditing = false
    presentViewController(imagePicker, animated: true, completion: nil)
}

func openCamera(){
    imagePicker.sourceType = .Camera
    imagePicker.showsCameraControls = true
    presentViewController(imagePicker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        // Image resizing
        let textViewWidth: CGFloat = self.textView.frame.size.width - 20
        let percentResize = textViewWidth / pickedImage.size.width
        let toBeExportedHeight = pickedImage.size.height * percentResize
        let resizedImage = ImageManipulationManager.sharedInstance.resizeImage(exportedWidth: Int(textViewWidth),exportedHeight: Int(toBeExportedHeight), originalImage: pickedImage)

        // Storage into TextView
        let attachment = NSTextAttachment()
        attachment.image = resizedImage
        let attString = NSAttributedString(attachment: attachment)
        textView.textStorage.insertAttributedString(attString, atIndex: textView.selectedRange.location)
        pastedImageLocations.append(textView.selectedRange.location)
        textView.selectedRange.location = textView.selectedRange.location + 1
        textView.textStorage.insertAttributedString(NSAttributedString(string: "\n"), atIndex: textView.selectedRange.location)
        textView.selectedRange.location = textView.selectedRange.location + 1
        textView.font = UIFont.systemFontOfSize(16.0)

        // Image Caching
        if let data = UIImageJPEGRepresentation(pickedImage, 0.50) {
            socketMessages.append(["data": data])
            haneke.set(value: data, key: String(unsafeAddressOf(attachment.image!)))
            print("Image cached as \"\(String(unsafeAddressOf(attachment.image!)))\"")
        }
    }
    dismissViewControllerAnimated(true, completion: nil)
    self.textView.becomeFirstResponder()
}

2 个答案:

答案 0 :(得分:1)

找到解决方案。

我不得不改变

dismissViewControllerAnimated(true, completion: nil)
self.textView.becomeFirstResponder()

dismissViewControllerAnimated(true) {
        self.textView.becomeFirstResponder()
    }

答案 1 :(得分:0)

您可以通过添加 -

进行一些更改
override func didPressLeftButton(sender: AnyObject?) {
    let cameraMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)

    let photoLibrary = UIAlertAction(title: "Photo Library", style: .Default, handler: { (UIAlertAction) in
        self.view.endEditing(true)    //**------ Add this
        self.openPhotoLibrary()
    })

    let takePhoto = UIAlertAction(title: "Open Camera", style: .Default, handler: { (UIAlertAction) in
        self.view.endEditing(true) //**------ Add this
        self.openCamera()
    })

    let cancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)

    cameraMenu.addAction(photoLibrary)
    cameraMenu.addAction(takePhoto)
    cameraMenu.addAction(cancel)

    self.presentViewController(cameraMenu, animated: true, completion: nil)
}