Matt Spoon
Matt Spoon

Reputation: 2830

Reading an image from NSUserDefaults

I have a swift app in which I have to save an image for a profile picture. I have saved it with the NSUserDefaults code below:

defaults.setObject(UIImagePNGRepresentation(imageView.image), forKey: "ProfilePicture")

My only question now is how do I read this from a file and then apply it to an UIImageView?

Thanks.

Upvotes: 1

Views: 395

Answers (1)

matt
matt

Reputation: 535149

UIImagePNGRepresentation creates an NSData version of the image. So when you extract it from user defaults, you will evidently need to turn an NSData into an image — which you can do quite simply, with UIImage(data:).

[I am also trying to resist the temptation to advise you not to put some huge blob of data like an image into NSUserDefaults - use the Application Support folder instead. But I'm not succeeding!]

Upvotes: 6

Related Questions