Iphone 如何从UILabel创建图像?
我目前正在iphone上开发一个简单的类似photoshop的应用程序。当我想展平我的图层时,标签处于良好的位置,但字体大小不好。 下面是我要展平的代码:Iphone 如何从UILabel创建图像?,iphone,objective-c,uiimage,uilabel,flatten,Iphone,Objective C,Uiimage,Uilabel,Flatten,我目前正在iphone上开发一个简单的类似photoshop的应用程序。当我想展平我的图层时,标签处于良好的位置,但字体大小不好。 下面是我要展平的代码: UIGraphicsBeginImageContext(CGSizeMake(widthDocument,widthDocument)); for (UILabel *label in arrayLabel) { [label drawTextInRect:label.frame]; } UIImage *image = UIGr
UIGraphicsBeginImageContext(CGSizeMake(widthDocument,widthDocument));
for (UILabel *label in arrayLabel) {
[label drawTextInRect:label.frame];
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
有人能帮我吗?发件人:
//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扩展和使用。credit@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)
发件人:
//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扩展和使用。credit@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)
您需要在上下文中呈现标签 替换
[label drawTextInRect:label.frame];
与
您需要为每个标签创建一个图像,因此将for循环移动到begin和end上下文调用之外您需要在上下文中呈现标签 替换
[label drawTextInRect:label.frame];
与
您需要为每个标签创建一个图像,因此将for循环移动到begin和end上下文调用之外您可以在任何CALayer上使用renderContext。它将在上下文上绘制视图的图层。以便您可以将其更改为图像。此外,如果在视图上渲染上下文,则其所有子视图都将绘制到上下文上。因此,在添加标签时,您可以将这些标签添加到containerView,而不是遍历所有标签。只需要在containerView上执行RenderContext。您可以在任何CALayer上使用RenderContext。它将在上下文上绘制视图的图层。以便您可以将其更改为图像。此外,如果在视图上渲染上下文,则其所有子视图都将绘制到上下文上。因此,在添加标签时,您可以将这些标签添加到containerView,而不是遍历所有标签。只需在该containerView上执行RenderContext。我创建了一个Swift扩展,用于将
UILabel
渲染为UIImage
:
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
}
}
编辑:改进的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()
}
}
用法很简单:
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 22))
label.text = bulletString
let image = UIImage.imageWithLabel(label)
我已经创建了一个Swift扩展,将
UILabel
呈现为UIImage
:
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
}
}
编辑:改进的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()
}
}
用法很简单:
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 22))
label.text = bulletString
let image = UIImage.imageWithLabel(label)
对于Swift,我使用以下UIView扩展:
// Updated for Swift 3.0
extension UIView {
var grabbedImage:UIImage? {
get {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0)
layer.render(in: UIGraphicsGetCurrentContext()!)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
}
}
对于Swift,我使用以下UIView扩展:
// Updated for Swift 3.0
extension UIView {
var grabbedImage:UIImage? {
get {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0)
layer.render(in: UIGraphicsGetCurrentContext()!)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
}
}
使用:
let iv = UIImageView(image: UIImage.imageFrom(text: "Test For \nYarik", width: 537.0, font: UIFont.systemFont(ofSize: 30), textAlignment: .center, lineBreakMode: .byWordWrapping))
iv.contentMode = .scaleAspectFit
使用:
let iv = UIImageView(image: UIImage.imageFrom(text: "Test For \nYarik", width: 537.0, font: UIFont.systemFont(ofSize: 30), textAlignment: .center, lineBreakMode: .byWordWrapping))
iv.contentMode = .scaleAspectFit
是的,我看过这篇文章,但用这个解决方案我无法得到高清晰度文件。但你让我明白我的问题在哪里!非常感谢@DJPlayer要获得用于视网膜显示的清晰图像,我们需要将方法中的第一行替换为以下行:
UIGraphicsBeginImageContextWithOptions(self.bounds.size,self.不透明,0.0)代码>如中所示及其接受的答案。也许您需要更新包含该信息的答案。如果您需要在UIGraphicsBeginImageContextWithOptions
中将透明图像设置为不透明的NO
,是否可以将标签渲染为其实际大小的3倍而不丢失清晰度?是,我看过这篇文章,但用这个解决方案我无法得到高清晰度文件。但你让我明白我的问题在哪里!非常感谢@DJPlayer要获得用于视网膜显示的清晰图像,我们需要将方法中的第一行替换为以下行:UIGraphicsBeginImageContextWithOptions(self.bounds.size,self.不透明,0.0)代码>如中所示及其接受的答案。也许您需要更新包含该信息的答案。如果您需要在UIGraphicsBeginImageContextWithOptions
中将透明图像设置为不透明的NO
,是否可以将标签渲染为实际大小的3倍而不丢失清晰度?请不要在没有任何解释的情况下粘贴代码。请不要粘贴没有任何解释的代码。