0

Am trying to add the text to an image which is taken from user.

In the first view, I created a textField and a button. Whatever the user enters in the textField and then presses the button, it will take to the second view where I have image view and I can set the image by using UIImagePickerController.

I have a label in the second view which text is set to textField.text.

I can drag that label where ever I want.

The code looks like:

class SecondView: UIViewController, UINavigationControllerDelegate,UIImagePickerControllerDelegate {

@IBOutlet weak var imageView: UIImageView!

var theLabel : UILabel!
var theText = ""
override func viewDidLoad() {
    super.viewDidLoad()
    theLabel = UILabel(frame: CGRect(x: self.view.bounds.width/2 - 100, y: self.view.bounds.height/2 - 50, width: 200, height: 100))
    theLabel.textAlignment = NSTextAlignment.Center
    self.view.addSubview(theLabel)
    var gesture = UIPanGestureRecognizer(target: self, action: Selector("dragMe:"))
    theLabel.addGestureRecognizer(gesture)
    theLabel.userInteractionEnabled = true
    theLabel.text = theText
}
func dragMe(gesture : UIPanGestureRecognizer) {
    let translation = gesture.translationInView(self.view)
    var label = gesture.view!
    label.center = CGPoint(x: label.center.x + translation.x, y: label.center.y + translation.y)
    gesture.setTranslation(CGPointZero, inView: self.view)
}
@IBAction func pickAnImage(sender: AnyObject) {
    var imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    self.presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
    imageView.image = image
    var bool = true
    self.dismissViewControllerAnimated(bool, completion: nil)
}

Now I want to add the label to an image. Since I can drag the label to what ever position, am able to place it on image.

But how to save the image with text?

Am I following wrong approach to get the output? Can anybody please help me to fix it.

Makyen
  • 31,849
  • 12
  • 86
  • 121
Pruthvi Hariharan
  • 551
  • 1
  • 6
  • 23

2 Answers2

1

You need to setup a CGContext and create a new image from the old plus the rendered text. See https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGContext/index.html

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
  • Hi,Thanks for reply. Am able to setup context and create a new image from the old, but am not able to render the text. Can you please tel me how to get the text. Am not getting which function to use to render the text. Please help me!!! Thanks in advance – Pruthvi Hariharan Dec 14 '14 at 03:36
0

check this answer, it's in Objective-c but I think it's easy to port these few lines or use it as it's https://stackoverflow.com/a/4334902/2165337

Community
  • 1
  • 1
Islam Ahmed
  • 539
  • 1
  • 5
  • 17