我正在尝试将文本写入图像,但当我这样做时,文本显示在错误的坐标中,我认为问题在于图像在 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/