如何从UILabel创建图像?

Jon*_*han 23 iphone objective-c flatten uiimage uilabel

我目前正在iphone上开发一个简单的Photoshop应用程序.当我想展平我的图层时,标签处于良好的位置,但字体大小不好.这是我的代码扁平化:

UIGraphicsBeginImageContext(CGSizeMake(widthDocument,widthDocument));

for (UILabel *label in arrayLabel) {
    [label drawTextInRect:label.frame];
}

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

有人可以帮帮我吗?

DJP*_*yer 47

来自:从UITextView或UILabel中提取UIImage会产生白色图像.

// iOS

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

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

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

    // Return the image.
    return image;
}
Run Code Online (Sandbox Code Playgroud)

// Swift扩展w /用法.信用@Heberti Almeida在下面评论

extension UIImage {
    class func imageWithLabel(label: UILabel) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0.0)
        label.layer.renderInContext(UIGraphicsGetCurrentContext()!)
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return img
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 22))
label.text = bulletString
let image = UIImage.imageWithLabel(label)
Run Code Online (Sandbox Code Playgroud)

  • @DJPlayer为了获得视网膜显示的清晰图像,我们需要用这一行替换你方法中的第一行:`UIGraphicsBeginImageContextWithOptions(self.bounds.size,self.opaque,0.0);`见[this question]( http://stackoverflow.com/q/4334233/2471006)及其接受的答案.也许您可能想要更新您的答案,包括该信息. (7认同)

Heb*_*ida 18

我创建了一个Swift扩展来将其呈现UILabelUIImage:

extension UIImage {
    class func imageWithLabel(label: UILabel) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0.0)
        label.layer.renderInContext(UIGraphicsGetCurrentContext()!)
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return img
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:改进Swift 4版本

extension UIImage {
    class func imageWithLabel(_ label: UILabel) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0)
        defer { UIGraphicsEndImageContext() }
        label.layer.render(in: UIGraphicsGetCurrentContext()!)
        return UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()
    }
}
Run Code Online (Sandbox Code Playgroud)

用法很简单:

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 22))
label.text = bulletString
let image = UIImage.imageWithLabel(label)
Run Code Online (Sandbox Code Playgroud)


wat*_*n12 5

您需要在上下文中呈现标签

更换

[label drawTextInRect:label.frame];
Run Code Online (Sandbox Code Playgroud)

[label.layer renderInContext:UIGraphicsGetCurrentContext()];
Run Code Online (Sandbox Code Playgroud)

并且您需要为每个标签创建一个图像,因此将for循环移动到开始和结束上下文调用之外