'Cannot subscript a value of type '[NSObject : AnyObject]' 索引类型为 'UIImagePickerController.Infokey'
'Cannot subscript a value of type '[NSObject : AnyObject]' with an index of type 'UIImagePickerController.Infokey'
我正在开发一款无需使用 IOS 客户端即可发送邮件的应用程序。
我决定使用 mailcore2 async api。
现在我可以发送电子邮件,但我想给它添加附件,我实际上是想点击一个按钮,打开图库,select 一张图片并获取它的路径。
我正在尝试这段代码,但是 2 和 7 行出现错误,我的“
let imageURL
和
let image
":
"Cannot subscript a value of type '[NSObject : AnyObject]' with an
index of type 'UIImagePickerController.InfoKey'
这是代码:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let imageURL = info[UIImagePickerController.InfoKey.referenceURL] as NSURL
let imageName = imageURL.path!.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first as! String
let localPath = documentDirectory.stringByAppendingPathComponent(imageName)
let image = info[UIImagePickerController.InfoKey.originalImage] as UIImage
let data = UIImagePNGRepresentation(image)
data.writeToFile(localPath, atomically: true)
let imageData = NSData(contentsOfFile: localPath)!
let photoURL = NSURL(fileURLWithPath: localPath)
let imageWithData = UIImage(data: imageData)!
picker.dismiss(animated: true, completion: nil)
}
您使用的是旧版本 Swift,以获取图像 URL:
let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL
建议您以后升级到Swift5.0
我正在开发一款无需使用 IOS 客户端即可发送邮件的应用程序。 我决定使用 mailcore2 async api。 现在我可以发送电子邮件,但我想给它添加附件,我实际上是想点击一个按钮,打开图库,select 一张图片并获取它的路径。 我正在尝试这段代码,但是 2 和 7 行出现错误,我的“
let imageURL
和
let image
":
"Cannot subscript a value of type '[NSObject : AnyObject]' with an index of type 'UIImagePickerController.InfoKey'
这是代码:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let imageURL = info[UIImagePickerController.InfoKey.referenceURL] as NSURL
let imageName = imageURL.path!.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first as! String
let localPath = documentDirectory.stringByAppendingPathComponent(imageName)
let image = info[UIImagePickerController.InfoKey.originalImage] as UIImage
let data = UIImagePNGRepresentation(image)
data.writeToFile(localPath, atomically: true)
let imageData = NSData(contentsOfFile: localPath)!
let photoURL = NSURL(fileURLWithPath: localPath)
let imageWithData = UIImage(data: imageData)!
picker.dismiss(animated: true, completion: nil)
}
您使用的是旧版本 Swift,以获取图像 URL:
let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL
建议您以后升级到Swift5.0