pm200107
pm200107

Reputation: 313

create image from data cocoa swift 5

I have a set of Data coming from a request (AlamoFire) as per the image below: enter image description here

I know it's an image, the size of the data is correct. I'd like to create an image from this and tried:

NSImage(data: result) or

NSImage.init(data:result)

Without success. It is possible to do what I try to achieve?

EDIT: to answer Larme on Self.coverImage: enter image description here

Upvotes: 0

Views: 544

Answers (1)

Diego Jiménez
Diego Jiménez

Reputation: 1526

This is a iOS snippet, if is useful for anyone:

// Swift
// Image to data
let image = UIImage(named: "sample")
let data = image?.pngData()
let data = image?.jpegData(compressionQuality: 0.9)

// Data to Image
let uiImage: UIImage = UIImage(data: imageData)

// this method is deprecated
var imageData: Data = UIImagePNGRepresentation(image)

Upvotes: -2

Related Questions