Hi 👋
I am trying to save/load UIImage and struggling with native part of implementation.
swift code would be something like
Copy code
func writeImage(name: String, uiImage: UIImage) {
let data = uiImage.pngData()
let fileUrl = cachesDirectoryUrl.appendingPathComponent("\(name).png")
let filePath = fileUrl.path
fm.createFile(atPath: filePath, contents: data)
}
but in kotlin I don’t get where is my pngData or something similar. Sorry if it was discussed somewhere, I did my best to search but failed 🙂
Kirill Kharkov
11/07/2022, 2:37 AM
Alright, I think I found it
Copy code
val data: NSData? = UIImagePNGRepresentation(avatarImage.image)
val filePath = NSURL("\\$file", relativeToURL = cachesDirectoryUrl).path ?: return
fileManager.createFileAtPath(filePath, data, null)
I still don’t quite get the logic behind such deviation in some APIs, so would appreciate to be enlightened 🙂
r
russhwolf
11/07/2022, 2:47 AM
Kotlin talks to Objective-C, not Swift. Some Apple APIs have Swift and Objective-C versions that are a little different. You can browse the Apple Developer Docs site in Objective-C mode to get a better sense of what things will look like from Kotlin.