Ios 向图像中的点添加文本
我正在使用UIApgestureRecognitor获取UIImageView中的坐标,然后使用该坐标作为向图像添加文本的位置。问题是,文本没有准确地添加到我在UIImageView上提到的位置 你能帮我解决这个问题或给我一些建议吗Ios 向图像中的点添加文本,ios,swift,swift3,uiimageview,Ios,Swift,Swift3,Uiimageview,我正在使用UIApgestureRecognitor获取UIImageView中的坐标,然后使用该坐标作为向图像添加文本的位置。问题是,文本没有准确地添加到我在UIImageView上提到的位置 你能帮我解决这个问题或给我一些建议吗 @IBOutlet var imageView: UIImageView! override func viewDidLoad() { super.viewDidLoad() let documentsPath =
@IBOutlet var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
imageView.image = UIImage(named: documentsPath + "/bach.jpg")
let imageViewTapped = UITapGestureRecognizer(target: self, action: #selector(self.tapAction(_:)))
imageViewTapped.numberOfTouchesRequired = 1
imageViewTapped.delegate = self
imageView.addGestureRecognizer(imageViewTapped)
}
func tapAction(_ sender: UITapGestureRecognizer) {
let point = sender.location(in: self.imageView)
print("x \(point.x) y \(point.y) ")
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let image = UIImage(named: documentsPath + "/bach.jpg")!
let newImage = textToImage(drawText: "㊗️Hello", inImage: image, atPoint: CGPoint(x: point.x, y: point.y))
imageView.image = newImage
}
// Add text to image
func textToImage(drawText text: NSString, inImage image: UIImage, atPoint point: CGPoint) -> UIImage {
let textColor = UIColor.white
let textFont = UIFont(name: "Helvetica Bold", size: 300)!
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))
text.draw(at: point, withAttributes: textFontAttributes)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
以png格式直接在文档中保存图像的代码。如果您想以jpeg格式保存它,只需将UIImagePNGRE表示行更改为UIImageJPEGRepresentation(图像,0.9)
以png格式直接在文档中保存图像的代码。如果您想以jpeg格式保存它,只需将UIImagePNGRE表示行更改为UIImageJPEGRepresentation(图像,0.9)
尝试将image.draw(in:CGRect(origin:CGPoint.zero,size:image.size))替换为image.draw(in:imageView.frame)@AravindAR其非沃克林,更改后基础图像变小尝试将image.draw(in:CGRect(origin:CGPoint.zero,size:image.size))替换为image.draw(in:imageView.frame)@AravindAR其非沃克林,更改后的基础图像变小再次感谢您。还有一个问题,我应该如何保存编辑过的图像。你的解决方案是创建一个子视图,对吗?不需要子视图,首先你想把它保存在哪里?我的意思是你想把它保存在文档目录或图库中吗?谢谢你的回复,只想把它保存在文档目录中。再次感谢你。还有一个问题,我应该如何保存编辑过的图像。你的解决方案是创建一个子视图,对吗?不需要子视图,首先你想把它保存在哪里?我的意思是,你想把它保存在文档目录或图库中吗?谢谢你的回复,只想把它保存在文档目录中。你真是太好了,帮了我的忙,但是那是保存编辑图像的代码吗?。例如,我触摸屏幕上的某个位置,并使用上面的代码向该位置添加文本或其他内容。最后将所有这些保存到png文件中。我运行了你的代码,它没有保存没有任何文本的图像我不清楚你在说什么,你想保存图像和文本它有什么吗?对不起,因为我的英语不好。“是否将图像与文本一起保存它所具有的内容?”← 是的,你真是太好了,帮了我的忙,但是那代码能保存编辑过的图像吗?。例如,我触摸屏幕上的某个位置,并使用上面的代码向该位置添加文本或其他内容。最后将所有这些保存到png文件中。我运行了你的代码,它没有保存没有任何文本的图像我不清楚你在说什么,你想保存图像和文本它有什么吗?对不起,因为我的英语不好。“是否将图像与文本一起保存它所具有的内容?”← 对
class ViewController1: UIViewController {
let lblNew = UILabel()
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
imageView.image = UIImage(named: documentsPath + "/bach.jpg")
imageView.isUserInteractionEnabled = true
let imageViewTapped = UITapGestureRecognizer(target: self, action: #selector(self.tapAction(_:)))
imageViewTapped.numberOfTouchesRequired = 1
imageViewTapped.delegate = self as? UIGestureRecognizerDelegate
imageView.addGestureRecognizer(imageViewTapped)
}
func tapAction(_ sender: UITapGestureRecognizer) {
let point = sender.location(in: self.imageView)
print("x \(point.x) y \(point.y) ")
textToImage(drawText: "㊗️Hello", atPoint: CGPoint(x: point.x, y: point.y))
}
// Add text to image
func textToImage(drawText text: NSString, atPoint point: CGPoint) {
lblNew.frame = CGRect(x: point.x, y: point.y, width: 200.0, height: 10)
lblNew.textAlignment = .left
lblNew.text = text as String
lblNew.textColor = UIColor.white
imageView.addSubview(lblNew)
}
}
func saveImage()
{
if let image = UIImage(named: "test.png") {
if let data = UIImagePNGRepresentation(image) {
let filename = getDocumentsDirectory().appendingPathComponent("copy.png")
try? data.write(to: filename)
}
}
}
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory
}
Replace your saveImage method with the following code snippet.
func saveImage()
{
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, UIScreen.main.scale)
imageView.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
if let data = UIImagePNGRepresentation(image!) {
let filename = getDocumentsDirectory().appendingPathComponent("copy.png")
try? data.write(to: filename)
}
}