1

I'm making an Camera app on Xcode. I made 'Photo Library' and 'Camera' options to insert photo to the screen. After that I just want to write text on the picture. Text can be from the user as using textbook. So basically I need to add text on the picture like datestamp.

I didn't find any example for this so it'd be helpful for developers. Hope to reach goal.

This is my screen:

enter image description here

and this is my code:

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var PhotoLibrary: UIButton!
@IBOutlet weak var Camera: UIButton!        
@IBOutlet weak var ImageDisplay: UIImageView!    

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func PhotoLibraryAction(sender: UIButton) {

    let picker = UIImagePickerController()
    picker.delegate = self
    picker.sourceType = .PhotoLibrary        
    presentViewController(picker, animated: true, completion: nil)
}

@IBAction func CameraActıon(sender: UIButton) {

    let picker = UIImagePickerController()
    picker.delegate = self
    picker.sourceType = .Camera        
    presentViewController(picker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    ImageDisplay.image = info[UIImagePickerControllerOriginalImage] as? UIImage;
    dismissViewControllerAnimated(true, completion: nil)
}

func textToImage(drawText: NSString, inImage: UIImage, atPoint:CGPoint)->UIImage{

    // Setup the font specific variables
    let textColor: UIColor = UIColor.blackColor()
    let textFont: UIFont = UIFont(name: "Helvetica Bold", size: 35)!

    //Setup the image context using the passed image.
    UIGraphicsBeginImageContext(inImage.size)

    //Setups up the font attributes that will be later used to dictate how the text should be drawn
    let textFontAttributes = [
        NSFontAttributeName: textFont,
        NSForegroundColorAttributeName: textColor,
        ]

    //Put the image into a rectangle as large as the original image.
    inImage.drawInRect(CGRectMake(0, 0, inImage.size.width, inImage.size.height))

    // Creating a point within the space that is as bit as the image.
    let rect: CGRect = CGRectMake(atPoint.x, atPoint.y, inImage.size.width, inImage.size.height)

    //Now Draw the text into an image.
    drawText.drawInRect(rect, withAttributes: textFontAttributes)

    // Create a new image out of the images we have created
    let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()

    // End the context now that we have the image we need
    UIGraphicsEndImageContext()

    //And pass it back up to the caller.
    return newImage        
    }

if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
    ImageDisplay.image = textToImage("TextYouWantDraw", inImage: image, atPoint: CGPointMake(10,10))}
}
pedrouan
  • 12,762
  • 3
  • 58
  • 74
coskukoz
  • 332
  • 2
  • 20
  • You can refer to this [thread](http://stackoverflow.com/a/28907930/5657370). – woogii Sep 22 '16 at 08:40
  • @woogii I'm trying it but I can't define the text. I mean how can I write text to insert to the picture? I guess it'll work if I can define some text. – coskukoz Sep 22 '16 at 11:21

1 Answers1

2

I guess you can get the image from photo library first and then pass it to the function which draws text into an Image.

 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

 if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
    ImageDisplay.image = textToImage("TextYouWantDraw", inImage: image, atPoint: CGPointMake(10,10))
 }

    dismissViewControllerAnimated(true, completion: nil)

}
woogii
  • 413
  • 3
  • 11
  • Maybe so easy error but I'm not really good at it so I'm gonna ask. I see these errors: *Consecutive declarations on a line must be separated by ';' * Expected declaration *'let' declarations cannot be computed properties *Computed property must have an explicit type *Variable with getter/setter cannot have an initial value – coskukoz Sep 22 '16 at 12:24
  • can you update your code on your question? I will look into it. – woogii Sep 22 '16 at 12:26
  • @CoşkunKozakbaş I updated my answer. Try that code. Optional binding statement should be located in the UIImagePickerController delegate method so that you can pass a selected image to 'textToImage' function as parameter. – woogii Sep 22 '16 at 12:41