swift - 如何在正确的坐标中快速将文本写入图像

标签 swift image text swift3 xcode8

我正在尝试将文本写入图像,但当我这样做时,文本显示在错误的坐标中,我认为问题在于图像在 Storyboard中被设置为 Aspect Fit,因为它大于屏幕尺寸,这是我的代码:

class ExteriorSketchViewController: UIViewController {

    // MARK: - Properties

    var lastPoint = CGPoint.zero

    // MARK: - Outlets

    @IBOutlet weak var template: UIImageView!

    // MARK: - Lifecycle methods

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            lastPoint = touch.location(in: self.view)
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let currentPoint = touch.location(in: self.view)
            lastPoint = currentPoint
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        template.image = textToImage(drawText: "x", inImage: template.image!, atPoint: lastPoint)
    }

    func textToImage(drawText text: NSString, inImage image: UIImage, atPoint point: CGPoint) -> UIImage {
        let textColor = UIColor.red
        let textFont = UIFont(name: "Helvetica Bold", size: 32)!

        let scale = UIScreen.main.scale
        UIGraphicsBeginImageContextWithOptions(image.size, false, scale)

        let textFontAttributes = [
            NSFontAttributeName: textFont,
            NSForegroundColorAttributeName: textColor,
            ] as [String : Any]
        image.draw(in: CGRect(origin: CGPoint.zero, size: image.size))

        let rect = CGRect(origin: point, size: image.size)
        text.draw(in: rect, withAttributes: textFontAttributes)

        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage!
    }

}

最佳答案

我想分享解决我问题的代码:

    import UIKit
    import AVFoundation

    class ExteriorSketchViewController: UIViewController {

        // MARK: - Properties

    var lastPoint = CGPoint.zero

    // MARK: - Outlets

    @IBOutlet weak var template: UIImageView!

    @IBOutlet weak var contentView: UIView! // this is a view inside the main view, contentView has the same size that template UIImageView

    // MARK: - Lifecycle methods

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            lastPoint = touch.location(in: self.view)
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let currentPoint = touch.location(in: self.view)
            lastPoint = currentPoint
        }
    }
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
            template.image = drawText(fromPoint: lastPoint, toPoint: lastPoint, text: "X")
        }

    func drawText(fromPoint:CGPoint,toPoint:CGPoint, text: String) -> UIImage {
            let scale = UIScreen.main.scale
            UIGraphicsBeginImageContextWithOptions(self.contentView.frame.size, false, scale)
            let imageRect = CGRect(x: 0, y: 0, width: self.contentView.frame.width, height: self.contentView.frame.height)
            let newRatio = AVMakeRect(aspectRatio: template.image!.size, insideRect: imageRect)
            template.image?.draw(in: newRatio)

            tool.center = toPoint

            let textColor = UIColor.red
            let textFont = UIFont(name: "Helvetica Bold", size: 17)!

            let textFontAttributes = [
                NSFontAttributeName: textFont,
                NSForegroundColorAttributeName: textColor,
                ] as [String : Any]

            let textRect = CGRect(x: fromPoint.x, y: fromPoint.y, width: self.contentView.frame.width, height: self.contentView.frame.height)
            text.draw(in: textRect, withAttributes: textFontAttributes)
            let newImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return newImage
        }
}

关于swift - 如何在正确的坐标中快速将文本写入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43060859/

相关文章:

ios - 在 iOS13 上强制 QLPreviewController 进入编辑模式

swift - 更改自定义表格 View 单元格中选择的按钮图像

javascript - 使用相同图像映射的多个图像 - 如何在点击时返回正确的图像引用?

javascript - 图像未裁剪为正确尺寸

jquery - 使用 jQuery 实现内联文本元素的交叉淡入淡出

python - 写入文本文件的最后一行?

html - 在旋转的 div 的另一边显示文本和 bgcolor

ios - While 循环在 Swift 中无法正确检查

swift - 处理 cloudkit 错误时未捕获的异常 'NSInvalidArgumentException'

javascript - 正确缩放图像但适合 div