Coşkun Kozakbaş Coşkun Kozakbaş - 2 months ago 29
Swift Question

How to Insert Text on Taken Picture by Swift?

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:
click here for image

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))}
}

Answer

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

 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)

}