gpt4 book ai didi

iphone - 如何从 UILabel 创建图像?

转载 作者:行者123 更新时间:2023-11-30 13:10:44 25 4
gpt4 key购买 nike

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

UIGraphicsBeginImageContext(CGSizeMake(widthDocument,widthDocument));

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

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

有人可以帮助我吗?

最佳答案

来自:pulling an UIImage from a UITextView or UILabel gives white image .

//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;
}

//带有用法的 Swift 扩展。在下面的评论中注明@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
}
}

用法:

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

关于iphone - 如何从 UILabel 创建图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38765857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com