【发布时间】:2016-01-10 04:26:34
【问题描述】:
我正在尝试获取从 Swift 库中导入的图像的 URL,以便使用 transferFile(_:metadata) 将其发送到 Apple Watch,但在 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);
}
我收到了 *imageName 和 *localPath 的错误,因为它说:
'lastPathComponent' 不可用:改为在 NSURL 上使用 lastPathComponent。 'stringByAppendingPathComponent' 不可用:在 NSURL 上使用 URLByAppendingPathComponent。
但我无法在 Swift 2.0 和 Xcode 7 中正确使用它。我哪里出错了?
【问题讨论】:
-
错误信息告诉你该怎么做。 SO上有很多例子。
-
@EricD。我尝试在代码中将值更改为 NSURL,但这在测试应用程序时不起作用。
标签: ios objective-c swift nsurl