0
我目前正在設計一個Realm數據庫管理應用程序,我已經成功地創建和檢索了一個對象。我遇到的問題是更新/編輯 - 特別是更新用戶上傳的UIImage。藉助Realm,我可以保存圖像的路徑,然後通過加載該路徑(在文檔目錄中)來檢索它。UIImageJPEGRepresentation | generateJPEGRepresentation將圖像保存爲零? Swift 3
當用戶試圖保存更改的圖像時,出於某種奇怪的原因,UIImageJPEGRepresentation
將更改的圖像保存爲零,從而刪除用戶的圖像。這很奇怪,因爲數據對象的初始創建存儲它就好了。
我試圖檢查圖像是否正在通過一些調試正確傳遞,並發現它這樣做很好,正確的路徑被保存。
這裏是我的更新方法:
func updateImage() {
let documentsDirectoryURL = try! FileManager().url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileURL = documentsDirectoryURL.appendingPathComponent("\(selectedPicPath!)")
if FileManager.default.fileExists(atPath: fileURL.path) {
do {
if profilePic.image != nil {
let image = profilePic.image!.generateJPEGRepresentation()
try! image.write(to: fileURL, options: .atomicWrite)
}
} catch {
print(error)
}
} else {
print("Image Not Added")
}
}
任何人都可以看到任何問題?
您要保存JPEG數據表示爲png格式 –
是的,既會工作,我想告訴你, 嘗試一下。 – 2017-02-25 02:15:59
當然它會保存,但一個JPEG文件是一個JPEG文件。如果你想要一個PNG文件,你需要使用'UIImagePNGRepresentation' –