将UILabel转换为UIImage会导致错误的大小和质量差

时间:2017-01-11 06:21:02

标签: ios objective-c uiimage uilabel uigraphicscontext

我的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,底部是生成的图像,请参阅差异

Single Responsibility Principle

0 个答案:

没有答案