9

I'm trying to take a screen shot of my app's current view and save it to photo album (to then be emailed or MMS'ed).

UIGraphicsBeginImageContext(self.view.bounds.size); 

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);

This works but the resulting image apears to be larger (533x800px) and heavily compressed when I email it from the photo library.

I've tried first writing the UIImage to file and then saving to album but still get the same issue.

If I used the in-built screenshot functionality on the iPhone the view saves correctly to photo album at 320x480 but the above code appears to save a larger image for some reason?

Thanks!

wuwongy
  • 91
  • 1
  • 2

2 Answers2

22

I found a decent workaround, which is to essentially rewrap the UIImage as a PNG, then save the rewrapped version. Code looks something like this:

UIImage* im = [UIImage imageWithCGImage:myCGRef]; // make image from CGRef
NSData* imdata =  UIImagePNGRepresentation ( im ); // get PNG representation
UIImage* im2 = [UIImage imageWithData:imdata]; // wrap UIImage around PNG representation
UIImageWriteToSavedPhotosAlbum(im2, nil, nil, nil); // save to photo album
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
Ben Weiss
  • 221
  • 2
  • 3
  • 1
    This is the solution to the compression problem. Images will be stored as PNG's in your Photo Library. Test it by emailing the image after it's in your Photo Library. – Paul Solt Mar 18 '11 at 21:00
  • How to give name to the images? – Priyal Mar 19 '18 at 09:11
1

I had that same error, on my part, that was solved when I rounded the decimal points to be the same scale as the iPhone, try it, make sure the scale is 1.0, 2.0, etc and not 3.1, that will throw it off.

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175