Swift - Write Image from URL to Local File

前端 未结 6 665
一生所求
一生所求 2021-02-01 15:55

I\'ve been learning swift rather quickly, and I\'m trying to develop an OS X application that downloads images.

I\'ve been able to parse the JSON I\'m looking for into a

6条回答
  •  情深已故
    2021-02-01 16:49

    In swift 2.0, stringByAppendingPathComponent is unavailable, so the answer changes a bit. Here is what I've done to write a UIImage out to disk.

    documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
    if let image = UIImage(data: someNSDataRepresentingAnImage) {
        let fileURL = documentsURL.URLByAppendingPathComponent(fileName+".png")
        if let pngImageData = UIImagePNGRepresentation(image) {
            pngImageData.writeToURL(fileURL, atomically: false)
        }
    }
    

提交回复
热议问题