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