スイフトの場合:
let imageData = UIImagePNGRepresentation(selectedImage)
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let imagePath = paths.stringByAppendingPathComponent("cached.png")
if !imageData.writeToFile(imagePath, atomically: false)
{
println("not saved")
} else {
println("saved")
NSUserDefaults.standardUserDefaults().setObject(imagePath, forKey: "imagePath")
}
スウィフト 2.1 の場合:
let imageData = UIImagePNGRepresentation(selectedImage)
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let imageURL = documentsURL.URLByAppendingPathComponent("cached.png")
if !imageData.writeToURL(imageURL, atomically: false)
{
print("not saved")
} else {
print("saved")
NSUserDefaults.standardUserDefaults().setObject(imageData, forKey: "imagePath")
}
stringByAppendingPathComponent
Swift 2.1 では使用できないため、URLByAppendingPathComponent.
Get more info here を使用できます。