【问题标题】:How to get the edited image from UIImagePickerController in Swift?如何在 Swift 中从 UIImagePickerController 获取编辑后的图像?
【发布时间】:2014-10-22 08:11:54
【问题描述】:

我已经看到并阅读了documents of Apple,他们清楚地提到了密钥UIImagePickerControllerEditedImage。但是当我运行下面的代码时,我没有在字典中得到那个键。这背后的原因是什么?

我的代码:

func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:NSDictionary!)
    {
        println(info)

        //var tempImage:UIImage = info[UIImagePickerControllerOriginalImage] as UIImage
        var tempImage:UIImage? = info[UIImagePickerControllerEditedImage] as? UIImage

        profileButton.setImage(tempImage, forState: UIControlState(0))

        self.dismissViewControllerAnimated(true, completion:nil)

    }

println(info) 打印:

{
    UIImagePickerControllerMediaType = "public.image";
    UIImagePickerControllerOriginalImage = "<UIImage: 0x178295d60> size {960, 960} orientation 0 scale 1.000000";
    UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=2DDCE06B-6082-4167-A631-B3DF627055DF&ext=JPG";
}

仅供参考,我正在从库中挑选图像,我也尝试从相机中,但这个键仍然没有到来,但关于图像的其他数据正在到来。我正在使用 iOS8.0 构建 SDK。

如果字典中没有这样的键,我该如何使用UIImagePickerControllerEditedImage

【问题讨论】:

  • 你好像没有启用图片选择器的allowEditing属性,你需要先设置&lt;pickerInstanceName&gt;.allowEditing = true,然后才能将UIImagePickerController呈现给窗口

标签: swift ios8 uiimagepickercontroller


【解决方案1】:

SWIFT 3.0

添加UINavigationControllerDelegateUIImagePickerControllerDelegate

var imagePicker = UIImagePickerController()

@IBAction func btnSelectPhotoOnClick(_ sender: UIButton)
{
    let alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
    alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
        self.openCamera()
    }))

    alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { _ in
        self.openGallary()
    }))

    alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))

    imagePicker.delegate = self
    self.present(alert, animated: true, completion: nil)
}
func openCamera()
{
    if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera))
    {
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        imagePicker.allowsEditing = true
        self.present(imagePicker, animated: true, completion: nil)
    }
    else
    {
        let alert  = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}
func openGallary()
{
    imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
    imagePicker.allowsEditing = true
    self.present(imagePicker, animated: true, completion: nil)
}


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    picker.dismiss(animated: true, completion: nil)
//You will get cropped image here..
    if let image = info[UIImagePickerControllerEditedImage] as? UIImage
    {
        self.imageView.image = image
    }   
}

【讨论】:

  • 嗨@Anas你能告诉我如何使用swift挑选编辑过的视频。我可以使用普通视频,但无法编辑视频。
【解决方案2】:

你可以这样做。

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {

    let image = info[UIImagePickerControllerEditedImage] as UIImage
}

【讨论】:

  • 您正在获取键“UIImagePickerControllerEditedImage”的值,但它不存在于字典“info”中。这就是我面临的问题!
  • 我已经在装有 iOS 7 的 iPad 和装有 iOS 8 的模拟器上对此进行了测试,当您使用 println("\(info)") 时,它的工作原理与 UIImagePickerControllerEditedImage 一样,我确实注意到打印方式有所不同将didFinishPickingMediaWithInfo info: [NSObject : AnyObject] 更改为didFinishPickingMediaWithInfo info:NSDictionary! 时,如果您使用上述代码,是否会出现某种错误?你用的是什么版本的 Xcode?​​span>
  • 老兄!我在我的问题中也提到了响应,并且我还提到基本 SDK 是 8.0,所以很明显我使用的是 xcode 6X。顺便说一句,我使用的是 XCode 6.1(不是 Beta)
  • 嗨@Developer - 你真的需要勾选这个答案。
【解决方案3】:

我找到了问题的原因!这是因为“allowsEditing”属性设置为false!我已将其更改为 true,现在它工作正常。

【讨论】:

    【解决方案4】:

    Swift 4.2

    if let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
            // Use editedImage Here
        }
    

    【讨论】:

    • 我收到此错误无法使用“UIImagePickerController.InfoKey”类型的索引为“[String : AnyObject]”类型的值下标
    【解决方案5】:

    确保您允许在UIImagePickerController 中编辑图像。

    您可以允许UIImagePickerController 编辑UIImage,方法是这样做

    picker.allowsEditing = true

    【讨论】:

      猜你喜欢
      • 2015-09-06
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      • 2018-10-17
      • 2018-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多