Swift - Write Image from URL to Local File

前端 未结 6 654
一生所求
一生所求 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:24

    In Swift 3:

    Write

    do {
        let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let fileURL = documentsURL.appendingPathComponent("\(fileName).png")
        if let pngImageData = UIImagePNGRepresentation(image) {
        try pngImageData.write(to: fileURL, options: .atomic)
        }
    } catch { }
    

    Read

    let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    let filePath = documentsURL.appendingPathComponent("\(fileName).png").path
    if FileManager.default.fileExists(atPath: filePath) {
        return UIImage(contentsOfFile: filePath)
    }
    
    0 讨论(0)
  • 2021-02-01 16:31

    The following code would write a UIImage in the Application Documents directory under the filename 'filename.jpg'

    var image = ....  // However you create/get a UIImage
    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
    let destinationPath = documentsPath.stringByAppendingPathComponent("filename.jpg")
    UIImageJPEGRepresentation(image,1.0).writeToFile(destinationPath, atomically: true)
    
    0 讨论(0)
  • 2021-02-01 16:36

    UIImagePNGRepresentaton() function had been deprecated. try image.pngData()

    0 讨论(0)
  • 2021-02-01 16:39

    Update for swift 5

    just change filename.png to something else

    func writeImageToDocs(image:UIImage){
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
    
        let destinationPath = URL(fileURLWithPath: documentsPath).appendingPathComponent("filename.png")
    
        debugPrint("destination path is",destinationPath)
    
        do {
            try image.pngData()?.write(to: destinationPath)
        } catch {
            debugPrint("writing file error", error)
        }
    }
    
    func readImageFromDocs()->UIImage?{
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
    
        let filePath = URL(fileURLWithPath: documentsPath).appendingPathComponent("filename.png").path
        if FileManager.default.fileExists(atPath: filePath) {
            return UIImage(contentsOfFile: filePath)
        } else {
            return nil
        }
    }
    
    0 讨论(0)
  • 2021-02-01 16:48
    @IBAction func savePhoto(_ sender: Any) {
    
            let imageData = UIImagePNGRepresentation(myImg.image!)
            let compresedImage = UIImage(data: imageData!)
            UIImageWriteToSavedPhotosAlbum(compresedImage!, nil, nil, nil)
    
            let alert = UIAlertController(title: "Saved", message: "Your image has been saved", preferredStyle: .alert)
            let okAction = UIAlertAction(title: "Ok", style: .default)
            alert.addAction(okAction)
            self.present(alert, animated: true)
        }   
    }
    
    0 讨论(0)
  • 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)
        }
    }
    
    0 讨论(0)
提交回复
热议问题