2017-05-21 2 views
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) 
    } 
} 
+0

링크 된 튜토리얼은 NSString.draw()를 사용하여 이미지에 텍스트를 그리는 좋은 예입니다. 당신이 그것을 시도했을 때 무슨 일이 일어 났습니까? – Robert

+0

[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

+0

[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

답변

2

여기가이 모든 것을 만들 수있는 간단한 방법이 없지만, 귀하의 첫 번째 chanllenge에 대한 몇 가지 코드입니다.

enter image description here

class ViewController: UIViewController { 

    @IBOutlet weak var storyboardImageView: UIImageView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     if let image = createFinalImageText() { 
      storyboardImageView.image = image 
     } 
    } 

    func createFinalImageText() -> UIImage? { 

     let image = UIImage(named: "bear.jpeg") 

     let viewToRender = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.width)) // here you can set the actual image width : image.size.with ?? 0/height : image.size.height ?? 0 

     let imgView = UIImageView(frame: viewToRender.frame) 

     imgView.image = image 

     viewToRender.addSubview(imgView) 

     let textImgView = UIImageView(frame: viewToRender.frame) 

     textImgView.image = imageFrom(text: "Example text", size: viewToRender.frame.size) 

     viewToRender.addSubview(textImgView) 

     UIGraphicsBeginImageContextWithOptions(viewToRender.frame.size, false, 0) 
     viewToRender.layer.render(in: UIGraphicsGetCurrentContext()!) 
     let finalImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 

     return finalImage 
    } 

    func imageFrom(text: String , size:CGSize) -> UIImage { 

     let renderer = UIGraphicsImageRenderer(size: size) 
     let img = renderer.image { ctx in 
      let paragraphStyle = NSMutableParagraphStyle() 
      paragraphStyle.alignment = .center 

      let attrs = [NSFontAttributeName: UIFont(name: "HelveticaNeue", size: 36)!, NSForegroundColorAttributeName: UIColor.white, NSParagraphStyleAttributeName: paragraphStyle] 

      text.draw(with: CGRect(x: 0, y: size.height/2, width: size.width, height: size.height), options: .usesLineFragmentOrigin, attributes: attrs, context: nil) 

     } 
     return img 
    } 
} 

내가 도움이되기를 바랍니다.

관련 문제