首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >合并CATextLayer和UIImage

合并CATextLayer和UIImage
EN

Stack Overflow用户
提问于 2015-12-04 22:12:24
回答 1查看 145关注 0票数 0

我想把我的CATextLayer和UIImage合并成一个UIImage。我用的是这个代码:

代码语言:javascript
复制
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是零。有人能告诉我,我做错了什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-16 16:41:44

我使用这段代码来解决我的问题:

代码语言:javascript
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34098351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档