How do I save a UIImage to a file?

前端 未结 9 863
抹茶落季
抹茶落季 2020-12-07 13:13

If I have a UIImage from an imagePicker, how can I save it to a subfolder in the documents directory?

9条回答
  •  执念已碎
    2020-12-07 13:34

    In Swift 4:

    // Create path.
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    if let filePath = paths.first?.appendingPathComponent("MyImageName.png") {
        // Save image.
        do {
           try UIImagePNGRepresentation(image)?.write(to: filePath, options: .atomic)
        }
        catch {
           // Handle the error
        }
    }
    

提交回复
热议问题