.pngData()
將圖片以 PNG 格式載入變數,以便稍後另存成檔案func saveFishPicToFile(_ image: UIImage) {
print("store fish pic locally")
// Create Dir
let manager = FileManager.default
guard let url = manager.urls(for: .documentDirectory, in: .userDomainMask).first else {
return
}
//print(url.path)
let newFolderUrl = url.appendingPathComponent("saved")
do {
try manager.createDirectory(at: newFolderUrl, withIntermediateDirectories: true)
} catch {
print(error)
}
// Create file
let fileUrl = newFolderUrl.appendingPathComponent("\(newStampName).png")
print(fileUrl) // 編碼會變成 url 型態,所以中文字會變成 %E4%BD%A0 這種樣子
let pngData = image.pngData()
//let path = documentDirectoryPath()?.appendingPathComponent("/pics/\(newStampName).png")
try? pngData?.write(to: fileUrl)
print("save pic to \(fileUrl)")
}