【问题标题】:Superimpose a user filled out UITextView over an image with CGRect, not the same as combining two images?用CGRect在一张图片上叠加一个用户填写的UITextView,和合并两张图片不一样?
【发布时间】:2017-05-12 18:33:24
【问题描述】:

我一直在使用这个函数通过 CG rect 将两个图像组合在一起:

 let renderer = UIGraphicsImageRenderer(size: CGSize(width: frame.bounds.size.width, height: frame.bounds.size.height))

        img = renderer.image { ctx in


            let someSize = CGSize(width: imageView.bounds.size.width, height: imageView.bounds.size.height)



            currentImage.scaleImageToSize(newSize: someSize).draw(in: CGRect(x: 0, y: 35, width: frame.bounds.size.width, height: imageView.bounds.size.height))



            //textView?.draw(CGRect(x: 0, y: 0, width: textView.bounds.size.width, height: textView.bounds.size.height))


            frames = UIImage(named: framesAr)
            frames?.draw(in: CGRect(x: 0, y: 0, width: frame.bounds.size.width, height: frame.bounds.size.height))

        }

我使用这个 CGRect 函数将 currentImage (UIImage) 和 frame(UIImage) 组合成一个 UIImage 没问题,但是我尝试以相同的方式添加文本视图似乎不起作用。

我在图像前面有一个 UITextView 用于测试目的。我希望能够在 textView 中输入文本并使用上述函数中的注释行来绘制整个 textView 及其内容。

当图像被保存时,函数运行,但 textView 被简单地忽略。谷歌搜索会产生关于如何向图像添加字符串的结果,但不会以这种方式产生用户可填充的文本视图。

任何帮助将不胜感激!

标签: swift swift3


【解决方案1】:

我找到了一种可行的方法,但它更像是一种解决方法。

设置你的 textview 就像 ichamps 回答这个问题:

How do I get text from a Swift textView?

然后像这样将 textview 的内容打印到图像上:

     let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.alignment = .left

            let attrs = [NSFontAttributeName: UIFont(name: "Avenir book", size: 14)!, NSParagraphStyleAttributeName: paragraphStyle]



            let string = "\(myString)"
            string.draw(with: CGRect(x: 0, y: 0, width: frame.bounds.size.width, height: frame.bounds.size.height), options: .usesLineFragmentOrigin, attributes: attrs, context: nil)

【讨论】:

    猜你喜欢
    • 2011-11-04
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多