合并 CATextLayer 和 UIImage
Merge CATextLayer and UIImage
我想将 My CATextLayer 与 UIImage 合并为一个 UIImage。我正在使用此代码:
let textViewLayer = CATextLayer()
textViewLayer.frame = CGRect(origin: CGPoint(x: lockText.imagePositionX, y: lockText.imagePositionY), size: lockImageView.frame.size)
textViewLayer.string = lockText.textText
textViewLayer.font = lockText.textFont.fontName
textViewLayer.fontSize = lockText.textFontSize
textViewLayer.foregroundColor = lockText.color.CGColor
let textView = UIView(frame: lockImageView.frame)
textView.layer.addSublayer(textViewLayer)
lockImageView.addSubview(textView)
UIGraphicsBeginImageContext(lockImageView.frame.size)
textViewLayer.renderInContext(UIGraphicsGetCurrentContext()!)
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
但是 UIGraphicsGetCurrentContext 是 NIL。有人能告诉我我做错了什么吗?
我正在使用这段代码来解决我的问题:
UIGraphicsBeginImageContext(image.size);
image.drawInRect(CGRectMake(0, 0, image.size.width, image.size.height))
let rect = CGRectMake(lockText.imagePositionX, lockText.imagePositionY, image.size.width, image.size.height);
lockText.color.set()
let text = lockText.textText as NSString
text.drawInRect(rect, withAttributes: [NSFontAttributeName : lockText.textFont, NSForegroundColorAttributeName : lockText.color])
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
我想将 My CATextLayer 与 UIImage 合并为一个 UIImage。我正在使用此代码:
let textViewLayer = CATextLayer()
textViewLayer.frame = CGRect(origin: CGPoint(x: lockText.imagePositionX, y: lockText.imagePositionY), size: lockImageView.frame.size)
textViewLayer.string = lockText.textText
textViewLayer.font = lockText.textFont.fontName
textViewLayer.fontSize = lockText.textFontSize
textViewLayer.foregroundColor = lockText.color.CGColor
let textView = UIView(frame: lockImageView.frame)
textView.layer.addSublayer(textViewLayer)
lockImageView.addSubview(textView)
UIGraphicsBeginImageContext(lockImageView.frame.size)
textViewLayer.renderInContext(UIGraphicsGetCurrentContext()!)
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
但是 UIGraphicsGetCurrentContext 是 NIL。有人能告诉我我做错了什么吗?
我正在使用这段代码来解决我的问题:
UIGraphicsBeginImageContext(image.size);
image.drawInRect(CGRectMake(0, 0, image.size.width, image.size.height))
let rect = CGRectMake(lockText.imagePositionX, lockText.imagePositionY, image.size.width, image.size.height);
lockText.color.set()
let text = lockText.textText as NSString
text.drawInRect(rect, withAttributes: [NSFontAttributeName : lockText.textFont, NSForegroundColorAttributeName : lockText.color])
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();