stringByAppendingPathComponent不可用

时间:2015-09-03 06:45:53

标签: ios swift2

我正在为Swift 2更新我的代码,并且我遇到了我在本地为用户个人资料保存的图像文件路径问题。我不知道在哪里解决这个问题,我尝试过使用URLByAppendPathComponent,但我不确定我错过了什么

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
    self.dismissViewControllerAnimated(true, completion: { () -> Void in
    })

    profileImageView.image = image

    let fileManager = NSFileManager.defaultManager()

    var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] 

    var filePathToWrite = "\(paths)/profileImage.jpeg"

    var imageData : NSData = UIImageJPEGRepresentation(image, 1.0)!

    fileManager.createFileAtPath(filePathToWrite, contents: imageData, attributes: nil)

    let imageFile : PFFile = PFFile(name: "profileImage.jpeg", data: imageData)
    imageFile.save()

    let getImagePath = paths.stringByAppendingPathComponent("profileImage.jpeg")
    print("getImagepath: \(getImagePath)")

    let user = PFUser.currentUser()
    user.setObject(imageFile, forKey: "profileImage")
    user.saveEventually()
}

如果有人可以帮我弄清楚如何更新我的代码,我将非常感激!

谢谢!

3 个答案:

答案 0 :(得分:1)

let nsPath = Path as! NSString

现在NSPath拥有你想要的所有功能 - 在swift2.0中他们有点禁用隐式转换更多

答案 1 :(得分:0)

嘿,我遇到了同样的问题..我用.stringByAppendingPathComponent("profileImage.jpeg")改变了.stringByAppendingString("/profileImage.jpeg") ..

这里输入的东西是前锋/

答案 2 :(得分:-1)

替换此

var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] 

let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String