我正在使用UITapGestureRecognizer来获取UIImageView中的坐标,然后使用该坐标作为向文档添加文本的位置。文本没有完全添加到我触及UIImageView的地方的问题 你能帮我解决问题还是给我一些建议
@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!
}
答案 0 :(得分:2)
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)
}
}
答案 1 :(得分:1)
直接以png格式将图像保存在文档中的代码。如果你想将它保存在jpeg formate中,那么只需将UIImagePNGRepresentation行更改为UIImageJPEGRepresentation(图像,0.9)。
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
}
答案 2 :(得分:1)
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)
}
}