Swift - Write Image from URL to Local File

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

提交回复
热议问题