1
我有一個基本的相機應用程序。當我拍攝一張照片時,該圖像將傳遞到下面的控制器中。我希望能夠將文本添加到傳入的圖像中,並將其拖到視圖中,縮放等等,但我甚至無法將文本添加到圖像上。我已經嘗試了一堆現代教程在線like this for example,但沒有。如何將文本添加到Swift 3中的圖像?
任何人都可以提供的例子嗎?
class PhotoViewController: UIViewController {
override var prefersStatusBarHidden: Bool {
return true
}
var lastPoint = CGPoint.zero
var red: CGFloat = 1.0
var green: CGFloat = 1.0
var blue: CGFloat = 1.0
var brushWidth: CGFloat = 10.0
var opacity: CGFloat = 1.0
var swiped = false
var backgroundImage: UIImage?
let backgroundImageView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
backgroundImageView.frame = self.view.frame
backgroundImageView.contentMode = UIViewContentMode.scaleAspectFit
backgroundImageView.isUserInteractionEnabled = true
self.view.backgroundColor = UIColor.black
backgroundImageView.image = backgroundImage
view.addSubview(backgroundImageView)
let backButton: UIButton = UIButton(frame: CGRect(x: 20, y: 20, width: 25, height: 25))
let image = UIImage(named: "back_button")
backButton.setImage(image, for: .normal)
backButton.addTarget(self, action: #selector(handleBackButton), for: .touchUpInside)
view.addSubview(backButton)
let postButton = UIButton(type: .system)
postButton.setTitle("Post", for: .normal)
postButton.addTarget(self, action: #selector(uploadPost), for: .touchUpInside)
view.addSubview(postButton)
_ = postButton.anchor(nil, left: nil, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 12, rightConstant: 12, widthConstant: 50, heightConstant: 50)
let saveImageButton = UIButton(type: .system)
let saveImageButtonImage = UIImage(named: "save_camera")?.withRenderingMode(.alwaysTemplate)
saveImageButton.setImage(saveImageButtonImage, for: .normal)
saveImageButton.tintColor = .white
saveImageButton.addTarget(self, action: #selector(handleSaveImage), for: .touchUpInside)
view.addSubview(saveImageButton)
_ = saveImageButton.anchor(nil, left: view.leftAnchor, bottom: view.bottomAnchor, right: nil, topConstant: 0, leftConstant: 12, bottomConstant: 12, rightConstant: 0, widthConstant: 50, heightConstant: 50)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
swiped = false
if let touch = touches.first {
lastPoint = touch.location(in: self.view)
}
}
func drawLine(from fromPoint: CGPoint, to toPoint: CGPoint) {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0)
backgroundImageView.image?.draw(in: view.bounds)
let context = UIGraphicsGetCurrentContext()
context?.move(to: fromPoint)
context?.addLine(to: toPoint)
context?.setLineCap(CGLineCap.round)
context?.setLineWidth(brushWidth)
context?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
context?.setBlendMode(CGBlendMode.normal)
context?.strokePath()
backgroundImageView.image = UIGraphicsGetImageFromCurrentImageContext()
backgroundImageView.alpha = opacity
UIGraphicsEndImageContext()
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
swiped = true
if let touch = touches.first {
let currentPoint = touch.location(in: view)
drawLine(from: lastPoint, to: currentPoint)
lastPoint = currentPoint
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if !swiped {
// draw a single point
self.drawLine(from: lastPoint, to: lastPoint)
}
}
您鏈接的教程有一個很好的使用'NSString.draw()'在圖像上繪製文本的例子。當你嘗試時發生了什麼? – Robert
你可以去[Image1上的文本](http://stackoverflow.com/questions/28906914/how-do-i-add-text-to-an-image-in-ios-swift/30185702)或[Text on Image1](http://stackoverflow.com/questions/39457767/overlaying-text-on-image-using-swift) – Foolish
你可以去[在ImageView Swift 3上添加文本](http://stackoverflow.com/questions/) 39457767 /覆蓋文本圖像使用swift)或[如何在ImageView上添加文本](http://stackoverflow.com/questions/28906914/how-do-i-add-text-to-an- image-in-ios-swift/30185702) – Foolish