0

I want to transfer a UIImage from the main application to my keyboard extension. The only way I can think of to do this is by using app groups. (If there's another I'd love to know :) ) Does anybody know how this would work? Do I need to use Core Data? Thank you!

1 Answers1

0

You can do it if you turn it into an NSData object encoded in some image format, like this for PNG:

if let data = UIImagePNGRepresentation(image) {
    userDefaults?.set(data, forKey: "Image")
}

then to get the image back on the other side you would use

if let data = userDefaults?.value(forKey:"Image") {
    let image = UIImage(data: data)
}

or something like that, i just migrated to swift 3 so the methods are slightly different from earlier methods.

e.g

set(data, forKey:..)

instead of

setValue(data, forKey:..)

Note this is assuming you are using groups. I've used it and it seems to work fine.

Dan
  • 31
  • 2