0

I have a UILabel of size 180 x 180 and I am converting it in the image, but when I am saving it I am getting 540 x 540 image resolution, I need 180 x 180. Here is my code.

- (void)grabImage {
    // Create a "canvas" (image context) to draw in.
    UIGraphicsBeginImageContextWithOptions(_lbl1.bounds.size, _lbl1.opaque, 0.0);  // high res
    // Make the CALayer to draw in our "canvas".
    [[_lbl1 layer] renderInContext: UIGraphicsGetCurrentContext()];

    // Fetch an UIImage of our "canvas".
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    // Stop the "canvas" from accepting any input.
    UIGraphicsEndImageContext();


    NSData *pngData = UIImagePNGRepresentation(image);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image180.png"]; //Add the file name
    [pngData writeToFile:filePath atomically:YES]; //Write the file

}

The quality is poor, in attached image upper one is original UILable and at bottom is the generated image, see the difference

enter image description here

S.J
  • 3,063
  • 3
  • 33
  • 66
  • 1
    THis is because UIGraphicsBeginImageContextWithOptions(_lbl1.bounds.size, _lbl1.opaque, 0.0);. You should pass the screen scale and not 0.0. Reason for 540 size is 180 x 3 (which is the scale of the device you're on) – GeneCode Jan 11 '17 at 06:49
  • Please can you give the code snippet how to do that ...... – S.J Jan 11 '17 at 06:58
  • I passed the [[UIScreen mainScreen] scale] in place of 0.0 but still same – S.J Jan 11 '17 at 07:02
  • @GeneCode I passed 1.0 but image quality in poor..... what should I do to keep image quality high and resolution 180 x 180 – S.J Jan 11 '17 at 07:38
  • You should use mainScreen scale. Then just resize it manually i guess. Use this method: http://stackoverflow.com/questions/612131/whats-the-easiest-way-to-resize-optimize-an-image-size-with-the-iphone-sdk – GeneCode Jan 11 '17 at 07:45
  • @GeneCode I have attached the image see the image quality, no method is working – S.J Jan 11 '17 at 11:16
  • please [check this out](http://stackoverflow.com/a/4334902/3308174) – Pratik Jamariya Jan 11 '17 at 18:50

0 Answers0