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.