如何从Swift中的UIImagePickerController获取编辑过的图像?

Dev*_*per 6 uiimagepickercontroller swift ios8

我已经看过并阅读了Apple文档,他们已经明确提到了关键的UIImagePickerControllerEditedImage.但是当我运行下面的代码时,我没有在Dictionary中获得该键.这背后的原因是什么?

我的代码:

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)

    }
Run Code Online (Sandbox Code Playgroud)

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";
}
Run Code Online (Sandbox Code Playgroud)

仅供参考,我正在从图书馆中挑选图像,我也尝试使用相机,但这个密钥仍未出现,但有关图像的其他数据即将到来.我正在使用iOS8.0构建SDK.

因此,UIImagePickerControllerEditedImage如果字典中没有这样的密钥,我该如何使用呢?

ana*_*s.p 8

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
    }   
}
Run Code Online (Sandbox Code Playgroud)


sba*_*row 7

你可以这样做.

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

    let image = info[UIImagePickerControllerEditedImage] as UIImage
}
Run Code Online (Sandbox Code Playgroud)