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
[Text1 Image1] (http://stackoverflow.com/questions/28906914/how-do-i-add-text-to-an-image-in-ios-swift/30185702) 또는 [텍스트 온 Image1] (http://stackoverflow.com/questions/39457767/overlaying-text-on-image-using-swift) – Foolish
[ImageView Swift 3에 텍스트 추가]를 사용할 수 있습니다 (http://stackoverflow.com/questions/). 39457767/overlaying-text-on-image-swift) 또는 [ImageView에서 텍스트를 추가하는 방법] (http://stackoverflow.com/questions/28906914/how-do-i-add-text-to-an- image-in-ios-swift/30185702) – Foolish