I am trying to update the content of an image from xcassets (through code, since I should download the new image) I need this so I can try to dynamically update the splash screen image and home page image (both are referenced from the same file in xcassets). This is because splash screen can only reference a locally present image.
I tried the following, but it does not work. Any help is appreciated.
var urlString = Bundle.main.resourcePath
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let filename = URL(fileURLWithPath: urlString!)
let fileURL = filename.appendingPathComponent("[email protected]")
if let pngImageData = image.pngData() {
try! pngImageData.write(to: fileURL, options: .atomic)
And also:
var documentsUrlw: URL {
return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
}
let fileName = "image_splashscreen@3x"
let fileURL = documentsUrl.appendingPathComponent(fileName)
if let imageData = image.jpegData(compressionQuality: 1.0) {
try? imageData.write(to: fileURL, options: .atomic)
return fileName // ----> Save fileName
}
In both cases the image doesn't update.
After seeing this question, I've searched a little bit for similar questions. For example this one: question
It says inside the Bundle(
.xcassetsresources) you cannot write any file to it. So you cannot write a file aURLeven you have its documentation. So you have two options places when you saving/writing data:Caches/Documents.