我正在尝试获取从Swift中的Library导入的图像的URL以将其发送到Apple Watch,transferFile(_:metadata)
但我有两个错误NSURL
.
这是我的代码:
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!)
{
imagePicked.image = image
let imageUrl = editingInfo[UIImagePickerControllerReferenceURL] as! NSURL
let imageName = imageUrl.path!.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String!
let localPath = documentDirectory.stringByAppendingPathComponent(imageName)
let image = editingInfo[UIImagePickerControllerOriginalImage]as! UIImage
let data = UIImagePNGRepresentation(image)
data!.writeToFile(localPath, atomically: true)
let photoURL = NSURL(fileURLWithPath: localPath)
self.dismissViewControllerAnimated(true, completion: nil);
}
Run Code Online (Sandbox Code Playgroud)
我在使用*imageName和*localPath时遇到错误,因为它说:
'lastPathComponent'不可用:改为使用NSURL上的lastPathComponent.'stringByAppendingPathComponent'不可用:改为在NSURL上使用URLByAppendingPathComponent.
但我无法在Swift 2.0和Xcode 7中做到正确.我哪里出错了?