gpt4 book ai didi

iphone - 如何从 UILabel 创建图像?

转载 作者:行者123 更新时间:2023-11-28 08:33:18 26 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/38382736/

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