1

I'm generating an UIImage as such:

//scale UIView size to match underlying UIImage size
float scaleFactor = 10.0

UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, scaleFactor);

[self.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage* image = UIGraphicsGetImageFromCurrentImageContext();

The UIImage has a size of 3200x2400, which is what I want. However, when I convert to PNG format to send as an email attachment:

NSData* data = UIImagePNGRepresentation(image);

MFMailComposeViewController* controller;
...
[controller addAttachmentData:data mimeType:mimeType fileName:.fileName];

I end up with and image that is 720 ppi and thus ~12.8mb. Which is way too large.

I don't know where the 720 ppi is coming from, the UIImage is generated from an image that is 72 ppi. It must have something to do with:

UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque,scaleFactor);

I need to create an UIImage from a UIView based on the underlying UIImage (which is much larger than the UIView's bounds), but I need to maintain the original ppi. 720 ppi is far too impractical for an email attachment.

Any thoughts?

PleaseHelp
  • 315
  • 1
  • 2
  • 15

3 Answers3

3

Your scaleFactor is too high which results in large image data . Decrease scaleFactor and then take screenshot.

Basically it should be

float scaleFactor = 1.0;

Convert into PNG like:

 NSData *imageData = UIImagePNGRepresentation(imagehere);

Attach imageData to mail.

EDIT : resize image like this:

 UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 1.0);
 [yourimageview.image drawInRect:CGRectMake(0,0,self.bounds.size)];
 UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • The problem with that is that the UIView's size is 320x240. It's subview is basically just a UIImage (UIImageView) whose size is 3200x2400. I want to create the UIImage from the UIView to match the underlying UIImage's size. Problem is, I'm ending up with a PNG, converted, from the resulting UIImage that has a 720 ppi resolution. This, of course, results in a email attachment of 12.8 mb. If the image was 72 ppi, it would be ~1.5 mb. – PleaseHelp Nov 21 '12 at 09:54
1

As per eagle.dan.1349's recommendation, I tried the following:

-(UIImage*)convertViewToImage
{
    UIImage* retVal = nil;

    //create the graphics context
    CGSize imageSize = targetImage.size;
    CGSize viewSize = self.bounds.size;

    //CGSize cvtSize = CGSizeMake(imageSize.width/viewSize.width,            imageSize.height/viewSize.height);
    float scale = imageSize.width/viewSize.width;

    UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, scale);

    //write the contents of this view into the context
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];

    //get the image
    retVal = UIGraphicsGetImageFromCurrentImageContext();

    //close the graphics context
    UIGraphicsEndImageContext();

    NSData* data = UIImageJPEGRepresentation(retVal, 0.0);
    [retVal release];
    retVal = [UIImage imageWithData:data];

    return retVal;
}

*Later on I perform:

NSData* data = UIImagePNGRepresentation(image);

However, as I mentioned, this still results in an image of 5.8 MB, so I suspect somewhere in the neighborhood of 300 ppi.

I need a UIImage, created from a UIView, at the resolution and size I require (72 ppi,3200X2400). There must be a way of doing this.

PleaseHelp
  • 315
  • 1
  • 2
  • 15
  • The above answer is NOT the answer, its just the only way I could include a code snippet in a response. – PleaseHelp Nov 21 '12 at 14:31
  • The underlying image started life out as 72 ppi, 3200x2400. Thus, it doesn't make sense for the resulting image to increase the resolution 10 fold – PleaseHelp Nov 21 '12 at 14:46
0

Firs, I wonder how your device don't cry with bloody tears from such HD images. When I worked on image-related project, such high resolution in PNG caused many problems with social network sharing and sending in email, so we moved to JPEG. In addition, it is generally not recommended to send images on web in PNG, better make it JPEG with proper compression. However, if you are required to use PNG you can make this kind of trick: first convert it to JPEG data, init your image with this data and than convert it to PNG.

Edit: In addition, try setting just context size you need 320X240 and scale not to 10, but to 0 for system to determine required scale. It may help. Then just scale your resulting UIImage once more.

eagle.dan.1349
  • 611
  • 1
  • 8
  • 20
  • That worked a little, but I still ended up with an image ~5.8 MB, so I suspect something in the 300 ppi range. I added an answer below to show the code I used. – PleaseHelp Nov 21 '12 at 14:25