1

I want to assign more than one value to the variable. I have no idea how to do. Basically I want to put multiple texts on the image.

This is single assignment code(which is working fine):

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

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        ImageDisplay.image = textToImage("HERE IS FIRST LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 300))

    }
    dismissViewControllerAnimated(true, completion: nil)

}

I just want to assign more value to the ImageDisplay.image. Here I can show an (wrong) example:

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

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        ImageDisplay.image = textToImage("HERE IS FIRST LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 100))
        ImageDisplay.image = textToImage("HERE IS SECOND LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 200))
        ImageDisplay.image = textToImage("HERE IS THIRD LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 300))            
    }
    dismissViewControllerAnimated(true, completion: nil)

}

This is textToImage function:

    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: 200)!

    //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
}
2
  • where is textToImage ? Commented Sep 27, 2016 at 12:42
  • @UmairAfzal I have updated with textToImage function Commented Sep 27, 2016 at 12:44

3 Answers 3

1

Each time you call textToImage with "image" you are creating a new object with the provided string from the original image, but what you want is add the string to each previously build image. So you can do this :

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

   if var image = info[UIImagePickerControllerOriginalImage] as? UIImage {
     image = textToImage("HERE IS FIRST LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 100))
     image = textToImage("HERE IS SECOND LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 200))
     image = textToImage("HERE IS THIRD LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 300))

     ImageDisplay.image = image            
  }
  dismissViewControllerAnimated(true, completion: nil)
}
Sign up to request clarification or add additional context in comments.

1 Comment

The easiest way at all. Works fine
1

My guess would be to chain the calls to textToImage(...) on a temporary UIImage:

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

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        var tmpImg = textToImage("HERE IS FIRST LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 100))
        tmpImg = textToImage("HERE IS SECOND LABEL", inImage: tmpImg, atPoint: CGPoint( x: 400, y: 200))
        tmpImg = textToImage("HERE IS THIRD LABEL", inImage: tmpImg, atPoint: CGPoint( x: 400, y: 300))  
        ImageDisplay.image = tmpImg
    }
    dismissViewControllerAnimated(true, completion: nil)
}

Comments

0

Your code mean this:

let a = textToImage("HERE IS FIRST LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 100))
ImageDisplay.image = a
let b = textToImage("HERE IS SECOND LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 200))
ImageDisplay.image = b
let c = textToImage("HERE IS THIRD LABEL", inImage: image, atPoint: CGPoint( x: 400, y: 300))
ImageDisplay.image = c

Your ImageDisplay.image stores only the last data (c variable)

Save a and b somewhere too to use them later. or change textToImage function to a new one.

EDIT THIS CODE WORKS (I HAVE TESTED IT):

//..somewhere
   guard let img = UIImage(named:"telephone40.png") else {
            return
        }//UIImage.init()
        //        img.size = CGSizeMake(500,500)
        var tmpImg = textToImage(["HERE IS FIRST LABEL","HERE IS SECOND LABEL","HERE IS THIRD LABEL"], inImage: img, atPoints: [CGPoint( x: 0, y: 10),CGPoint( x: 20, y: 30),CGPoint( x: 40, y: 50)])
//result <<<<    

//function:

    func textToImage(drawTexts: [NSString], inImage: UIImage, atPoints:[CGPoint])->UIImage{

        // Setup the font specific variables
        let textColor: UIColor = UIColor.blackColor()
        let textFont: UIFont = UIFont(name: "Helvetica Bold", size: 200)!
        //Setups up the font attributes that will be later used to dictate how the text should be drawn

        let textFontAttributes = [
            NSFontAttributeName: textFont,
            NSForegroundColorAttributeName: textColor,
            ]
        //Setup the image context using the passed image.
        UIGraphicsBeginImageContext(inImage.size)
        //Put the image into a rectangle as large as the original image.
        inImage.drawInRect(CGRectMake(0, 0, inImage.size.width, inImage.size.height))
        for i in 0..<drawTexts.count {
            let drawText = drawTexts[i]
            let atPoint = atPoints[i]



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

6 Comments

You are right ImageDisplay.image stores only the last data. But I want to show all texts in one image.
@coskukoz , changed a bit
I got an unresolved error here: for i in 0..drawTexts.count {.
@coskukoz fixed
Still something wrong. I add 'atPoints' here: inImage: image, atPoints: [CGPoint. Now there is no error but function does not work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.