iOS 14 background image not changing.

Hi all,

Can anyone tell me why the following code works perfectly when it runs the first time, or when the app reloads? It does not however change the VC background image, although I can see it step through the code. The image does load correctly when the app is swiped away and reloaded.

I am stumped. Any thoughts would be much appreciated.

Blessings, --Mark

Code Block
if CacheManager.getIsUserBackGround() {
//get a refrerence to the the documents for the app
let fileManager = FileManager.default
//get the path from Constants
let imagePath = Constants.getImage()
//check if not nil, and if not set the background pict
if imagePath != "" {
if fileManager.fileExists(atPath: imagePath!) {
DispatchQueue.main.async {
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.image = UIImage(contentsOfFile: imagePath!)!
backgroundImage.contentMode = UIView.ContentMode.scaleAspectFill
self.view.insertSubview(backgroundImage, at: 0)
}
}else{
DispatchQueue.main.async {
print("error")
// Alerts.showAlert(title: "Attention", message: "A user selected image from the Photos Library has not been saved. \n \n Tap the Gear icon on the 'Lists' screen and select the 'Set background from Photos' option to save an image.", vc: self)
}
}
}

Accepted Reply

I make things WAY harder than they need to be!

I discovered that I was adding a view (and the memory overhead) each time I changed the image and when the app reset, it just used the last image saved.

The simple solution was to ignore the background view and add a UIImageView and change the image to that! Live and learn!

Replies

First of all, please clarify where your code exists?
Can you show whole code of the method where the code exists?

Second, please clarify when you expect the background to be updated.

Third, your code just adds new UIImageView every time it is called, does not change the image of the image view.
Hi,

Thanks for your response.

This code is in the initial viewController. A new image can be selected in the settings VC. As I said, it works fine the first time and and when I swipe away the app.

I call the method in viewWillAppear. It does run when the view activates, just does not update the image.

I would expect the image would be be boded with the image data saved in the defaults every time it is called.

Third: I call self.view.insertSubview(backgroundImage, at: 0) so that should insert the image. At least it does the first time and the the app restarts.

Thanks.
Blessings,
—Mark

so that should insert the image. 

A little difference. It inserts a new image view with the image.

it works fine the first time and and when I swipe away the app. 

Not working fine. It seemingly works only the first time.
Have you checked viewWillAppear is called ?

Did you try to call setNeedsDisplay on the whole view ?
@Claude31

Thanks for your reply.

Yes, viewWill Appertains is being called and setNeedsDisplay did not change the behavior.

I must be missing something obvious.

Thanks.
Blessings,
—Mark


I make things WAY harder than they need to be!

I discovered that I was adding a view (and the memory overhead) each time I changed the image and when the app reset, it just used the last image saved.

The simple solution was to ignore the background view and add a UIImageView and change the image to that! Live and learn!