In some reasons, you might want to store an UIImage, that is generated using Core Graphics, or from device’s Photo Gallery via UIImagePickerController, to a local file as PNG or a JPEG format.

By using UIImagePNGRepresentation() or UIImageJPEGRepresentation(), UIImage will be converted into Data so you can write it to local file easily.

Here is an example of a helper function that store an UIImage to a PNG file:

In the upper code, there is a call to fileURLInDocumentDirectory().
In this example, I store a file to Document Directory. You can replace that funcion by your own function getting the file URL of the location you want to store a file.

If you want to store your image as a JPEG, just replacing UIImagePNGRepresentation() as UIImageJPEGRepresentation()

Here is the code:

0.5 is JPEG quality. You can change it from 1.0 to 0.0.