UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 77 | Converting UIImage to JpegData

Forums > 100 Days of SwiftUI

Struggling with converting UIImage to the JpegData, any help for this?

if let jpegData = yourUIImage.jpegData(compressionQuality: 0.8) { try? jpegData.write(to: yourURL, options: [.atomic, .completeFileProtection]) }

What should I write to the "yourURL" ?

1      

Here's an excellent response from @roosterboy!

See -> Excellent Response

1      

I'm not sure why the last 2 people have provided links to instructions for posting questions clearly on this forum.

It is pretty clear what you are asking about given the title of your post, and taking a quick peek at the challenge instructions for Day 77

The yourURL should be replaced with the URL that you want to save your data to. In this project, it is a file in the File Manager. You can use something like this to store the URL of the file that you are trying to write your data to, and be able to read from the same URL later so you don't have to try to find it twice.

let savePath = FileManager.documentsDirectory.appendingPathComponent("NamedFaces")

Then you would use savePath in place of yourURL in the provided example.

You can get the data that represents your UIImage (so that it can easily be written to a file) by using

var jpegData = yourUIImage.jpegData(compressionQuality: 0.8)

Then when you read the data back from your file later, you can convert the data back into a UIImage by using something like

var yourUIImage = UIImage(data: jpegData)

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.