我的UILabel
大小为180 x 180
而我正在image
转换它,但是当我保存它时,我得到540 x 540
图像分辨率,我需要180 x 180
。这是我的代码。
- (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
}
质量差,在附加图像中,上面一个是原始UILable
,底部是生成的图像,请参阅差异