2014-10-22 96 views
2

我已经看到并阅读了documents of Apple他们已经清楚地提到了关键的UIImagePickerControllerEditedImage。但是当我运行我的下面的代码时,我没有得到该字典中的密钥。这背后的原因是什么?如何在Swift中从UIImagePickerController获取编辑后的图像?

我的代码:

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(信息)打印:

{ 
    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

+2

它好像你没有启用图像拾取的'allowEditing'属性,你需要设置' .allowEditing = true“,然后将”UIImagePickerController“呈现给窗口 – deoKasuhal 2015-10-23 00:21:50

回答

4

你可以这样做。

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

    let image = info[UIImagePickerControllerEditedImage] as UIImage 
} 
+0

您正在获取键”UIImagePickerControllerEditedImage“的值,但它没有出现在字典”info“中。这是我面临的问题! – Developer 2014-10-22 08:58:51

+0

我已经在iOS 7和iOS 8的模拟器上测试了这个功能,并且它在你运行println(“\(info)”时显示'UIImagePickerControllerEditedImage'''我注意到事物的方式有所不同在将'didFinishPickingMediaWithInfo info:[NSObject:AnyObject]'更改为'didFinishPickingMediaWithInfo info:NSDictionary!'时打印?如果您使用上述代码,您会遇到某种错误吗?你使用的是什么版本的Xcode? – sbarow 2014-10-22 09:07:46

+0

老兄!我在我的问题中也提到了这个响应,我也提到基础SDK是8.0,所以显然我使用xcode 6X。 顺便说一句我正在使用XCode 6.1(不是测试版) – Developer 2014-10-22 09:11:10

0

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

3

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 
    } 
} 
+0

嗨@Anas你能告诉我怎么用swift选择编辑的视频。我可以使用普通视频,但无法编辑。 – 2016-12-08 13:35:13

相关问题