1

I am creating an image that is then saved to the users camera roll. I would like to add some text to the image. Should I add the string to a UILabel then convert this to an image?

What is your suggestion? Thanks. (I am using Swift)

    let bottomImage = UIImage(named: "Image1.png")
    let topImage = UIImage(named: "Image2.png")
    let anotherImage = UIImage(named: "Image3.png")

    let newSize = CGSizeMake(2480, 3508)
    UIGraphicsBeginImageContext(newSize)

    bottomImage?.drawInRect(CGRectMake(0, 0, newSize.width/2, 500))
    topImage?.drawInRect(CGRectMake(newSize.width/2, 0, newSize.width/2, 500), blendMode: kCGBlendModeNormal, alpha: 0.8)
    anotherImage?.drawInRect(CGRectMake(newSize.width/2, 500, newSize.width/2, 500), blendMode: kCGBlendModeNormal, alpha: 0.8)

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()


    UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil)
Tom Coomer
  • 6,227
  • 12
  • 45
  • 82

1 Answers1

0

You want to use NSAttributedString drawInRect.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • How do I add the string to the NSAttributedString? – Tom Coomer Oct 31 '14 at 18:22
  • [`NSAttributedString initWithString`](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/index.html#//apple_ref/occ/instm/NSAttributedString/initWithString:) – i_am_jorf Oct 31 '14 at 18:37
  • I am trying to change the font using the code below and I am displayed an error message. `let textFont = [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)] let title = NSAttributedString(string: titleValue) let title = NSAttributedString(string: titleValue, attributes: textFont)` "Extra Argument 'string' in call – Tom Coomer Oct 31 '14 at 19:17
  • For `attributes` you want to pass a dictionary, so I *think* the swift version of that is `attributes: [NSFontAttributeName : textFont]`. – i_am_jorf Oct 31 '14 at 19:21
  • I have updated it to the following `let title = NSAttributedString(string: titleValue, attributes: [NSFontAttributeName : textFont])` and still have the same message. – Tom Coomer Oct 31 '14 at 19:23
  • Oh I see `textFont` is a dictionary. It's hard to read in the comments. I'm not 100% sure how you would write it in swift. – i_am_jorf Oct 31 '14 at 19:23
  • Any ideas how to create an attribute for Text Alignment? – Tom Coomer Oct 31 '14 at 19:48
  • Check out http://stackoverflow.com/questions/6992830/how-to-write-text-on-image-in-objective-c-iphone (it's obj c, but the concept is the same) – Anna Dickinson Oct 31 '14 at 21:20