我已经看过并阅读了documents of 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)
}
println(info)print:
{
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
。
答案 0 :(得分:7)
你可以这样做。
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let image = info[UIImagePickerControllerEditedImage] as UIImage
}
答案 1 :(得分:6)
SWIFT 3.0 :
添加UINavigationControllerDelegate
和UIImagePickerControllerDelegate
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
}
}
答案 2 :(得分:1)
我找到了问题的原因!因为属性“allowsEditing”被设置为 false !我已将其更改为 true ,现在工作正常。
答案 3 :(得分:1)
Swift 4.2
if let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
// Use editedImage Here
}
答案 4 :(得分:0)
确保您允许编辑UIImagePickerController
中的图像。
您可以通过以下操作允许UIImagePickerController
编辑UIImage
picker.allowsEditing = true