Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImage to NSData for Core Data in Swift 3.0

So previously, in Swift 2.2 I could use Entity.image = UIImagePNGRepresentation(UIImage(named: "Image Name")!)

But now in Swift 3.0 it appears that we now have to use an extra step. Entity.image = NSData(data: UIImagePNGRepresentation(UIImage(named: "Image Name")!)) Or the object equivalent of UIImage(name: "Image Name")

Is there a new way in Swift 3.0 to go from UIImage to NSData?

like image 378
Jason Brady Avatar asked Oct 02 '16 23:10

Jason Brady


People also ask

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .


1 Answers

Try this:

if let img = UIImage(named: "hallo.png") {
    let data = UIImagePNGRepresentation(img) as NSData?
}
like image 129
Quang Hà Avatar answered Oct 12 '22 14:10

Quang Hà