Iphone – UIImageWriteToSavedPhotosAlbum saves to wrong size and quality

iphone

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 320×480 but the above code appears to save a larger image for some reason?

Thanks!

Best Answer

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
Related Topic