0

New to coding, I've figured out how to let a user select a photo as their background with the following code

 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        backgroundImage.contentMode = .ScaleAspectFill
        backgroundImage.image = pickedImage
    }

    dismissViewControllerAnimated(true, completion: nil)
}

// Wallpaper function

@IBAction func wallpaperMenuPressed(sender: AnyObject) {

    imagePicker.allowsEditing = false
    imagePicker.sourceType = .PhotoLibrary

    presentViewController(imagePicker, animated: true, completion: nil)

}

And it works, still haven't figured out how to save it but will find out soon.

But, if I have a backgroundImage in all my views, how do I get it to set the same image for all of them?

Thanks

Khoury
  • 411
  • 1
  • 4
  • 21

2 Answers2

1

I would suggest making use of NSNotificationCenter.

In each of your view controllers where you need to apply the change you listen for a notification:

let nc = NSNotificationCenter.defaultCenter()
nc.addObserver(self, selector: "UpdateBackgroundImage:", name: "BackgroundImageChanged", object: image)

In each of those view controllers you'll need to implement the selector, UpdateBackgroundImage in this case. They would be similar to:

func UpdateBackgroundImage(notification: NSNotification) {
    let backgroundImage = notification.userInfo!["image"] as UIImage
    // Your code to assign image to background
}

After you save the image you can post a notification:

// Right after you assign backgroundImage.image = pickedImage  
let nc = NSNotificationCenter.defaultCenter()
let myDict = [pickedImage, "image"]
nc.postNotificationName("BackgroundImageChanged", object: nil, userInfo: myDict)

The nice thing about using NSNotificationCenter is that as you add new views that need to update the background images you just add the first bit of code. You don't need to update the code where you pick the images.

user212514
  • 3,110
  • 1
  • 15
  • 11
  • This seems like it would work, but I'm not sure how to implement it? – Khoury Apr 14 '16 at 05:03
  • Do you know how to persist the image? I know its to do with nsuserdefaults, but as soon as I quit the app, the image reverts to the original – Khoury Apr 15 '16 at 23:57
  • That's really a separate question. Here's the answer though: http://stackoverflow.com/questions/26171901/swift-write-image-from-url-to-local-file – user212514 Apr 16 '16 at 02:19
  • Oh sorry, Thanks for the help though – Khoury Apr 16 '16 at 04:56
1

I suggest you to create a BaseViewController in which common functionality of your app is there and also take a imageView in that.

After that inherit all your viewControllers form BaseViewController so that imageView also be inherited and you can set it and it will be same all of your viewControllers inherited from BaseViewController.

Mahendra
  • 8,448
  • 3
  • 33
  • 56
  • I can't do that because all my views each have their own controller file, thanks for your help though :) – Khoury Apr 14 '16 at 05:02