ios - 为什么每当我加载 UIImagePickerController View 时都会加载我的键盘?

标签 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()
}

最佳答案

找到解决方案。

我不得不改变

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

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

关于ios - 为什么每当我加载 UIImagePickerController View 时都会加载我的键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39284180/

相关文章:

iphone - 将 nsnumber 或 nsstring 放入 initWithLatitude :

ios - 以编程方式在 ViewController 中使用 NavigationController

objective-c - 如何在 iOS 中以圆形方向旋转 UIButton?

swift - 使用附加值初始化 swift 中的属性

ios - 使用 CAMERA(不是相册)时从 UIImagePickerController 获取缩略图

iphone - iOS 5 中相机叠加 View 上的触摸对焦?

ios - CoreData对象未填充值

swift - api调用无法开启精准落地

swift - iOS 14、Swift UI 2 更改 NavigationLink 内所选列表项的背景颜色

ios - UIImagePickerControllerReferenceURL 总是返回 nill