6

I am trying to save an UIImage object to an .jpeg file in device and I am using this code:

-(void)saveImageToDocumentsDirectory:(UIImage *)mimage withFileName:(NSString *)fileName 
{
    UIImageWriteToSavedPhotosAlbum(mimage,nil,nil,nil);
    NSData *dataForJPEGFile = UIImageJPEGRepresentation(mimage, 1.0);
    NSError *error2 = nil;
    if (![dataForJPEGFile writeToFile:[self getDirectoryFilePath:fileName] options:NSAtomicWrite error:&error2])
    {
        return;
    }
}

It will save the image of .jpeg type but take too much memory as compare to the UIImageWriteToSavedPhotosAlbum(mimage,nil,nil,nil) method, which saves the same image object in .jpeg type and same quality.

My question is why so..??

Bista
  • 7,869
  • 3
  • 27
  • 55
Sushobhit
  • 305
  • 4
  • 18
  • "Same quality" is subjective, have you tried to gradually decrease the image quality parameter until the sizes are about the same and the quality is still "the same" ? – A-Live Jan 12 '16 at 10:32
  • @A-Live when i decrease the image quality parameter the size also decreases and quality too. but i want to save the image as this UIImageWriteToSavedPhotosAlbum(mimage,nil,nil,nil) method does. how can i achieve this..?? – Sushobhit Jan 12 '16 at 10:41
  • Easy, use UIImageWriteToSavedPhotosAlbum. – A-Live Jan 12 '16 at 11:03
  • @A-Live if i use UIImageWriteToSavedPhotosAlbum then how can i get these image back for further use without using imagePicker..?? – Sushobhit Jan 12 '16 at 11:10
  • Using completionSelector parameter ? – A-Live Jan 12 '16 at 11:34
  • @A-Live thanks a lot for your help. now when I am saving the image using UIImageWriteToSavedPhotosAlbum in the device and after done some short of other task if user want to upload this image file to the server then I don't find how can i get this file at the time of uploading.. that the real problem now i am facing. – Sushobhit Jan 12 '16 at 12:24
  • Depends on your app design, either keep a copy of the UIImage in memory or on disk or ask the user to pick it again. I don't know if there's any kind of a persistent descriptor to get back to the image at the system media storage once it is saved, not likely. – A-Live Jan 12 '16 at 13:13
  • Was the solution for the issue found out? – Aravind Mariappan Sep 19 '18 at 12:35

1 Answers1

-1

You are setting the quality to be really high (1.0), I guess 0.6 or 0.7 would be more than enough for jpeg files as I read somewhere.

NSData *dataForJPEGFile = UIImageJPEGRepresentation(mimage, 0.65);

Maybe you can find the appropriate quality depending on your app use, for me sometimes 0.5 is more than enough.

Boda
  • 1,484
  • 1
  • 14
  • 28