Skip to content

Instantly share code, notes, and snippets.

@trilliwon
Last active January 12, 2024 14:45
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save trilliwon/5af1abe1a113148c23ecde8c08e181a6 to your computer and use it in GitHub Desktop.
Save trilliwon/5af1abe1a113148c23ecde8c08e181a6 to your computer and use it in GitHub Desktop.
Swift UIImage to Data, Data to UIImage
// Swift4
let image = UIImage(named: "sample")
let data = image?.pngData()
let data = image?.jpegData(compressionQuality: 0.9)
let uiImage: UIImage = UIImage(data: imageData)
// deprecated
// var imageData: Data = UIImagePNGRepresentation(image)
@ApolloRisky
Copy link

Thanks

@ZZhaiDev
Copy link

for swift 4:
let profileImage = UIImage(named:"profile")!
let imageData = profileImage.pngData()

@DGP13
Copy link

DGP13 commented Dec 5, 2018

profile.image.pngData() this is not working.

@jacobpitkin78
Copy link

It's profileImage.pngData(), not profile.image.pngData() (note the camel case, not the period)

@nikhil0011
Copy link

Please share how to compress PNG images using UIImagePNGRepresentation

@TheRadikalStyle
Copy link

Thank you

@sthorne
Copy link

sthorne commented Feb 6, 2021

Please share how to compress PNG images using UIImagePNGRepresentation

The PNG format includes compression by default

@mzm008
Copy link

mzm008 commented Dec 5, 2022

how to convert Image to Data ??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment