1

Is it possible to add a text string to an image when saving to camera roll?

for example photo is taken and saved to camera roll:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{


UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(image, nil,@selector(image:didFinishSavingWithError:contextInfo:), nil);

  //something here to add @"my pre defined text"; to saved image


}

I want to automatically add pre defined text to this saved image without any user input if possible?

Nothing comes up on the usual searches on SO and Google regarding what I am trying to do

JSA986
  • 5,870
  • 9
  • 45
  • 91

2 Answers2

1

Take a Look on my code:

-(UIImage *)addText:(UIImage *)img text:(NSString *)text1
{
    int w = img.size.width;
    int h = img.size.height; 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1);

    char* text = (char *)[text1 cStringUsingEncoding:NSASCIIStringEncoding];
    CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);

    CGContextSetRGBFillColor(context, 255, 255, 255, 2);

    CGContextShowTextAtPoint(context, 10, 170, text, strlen(text));

    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    return [UIImage imageWithCGImage:imageMasked];
}

And improve with:

- (IBAction)drawImage:(id)sender {
    NSString *text1 = myTextField.text;
    UIImage *updatedImg = [self addText:myImageView.image text:text1]; 
    [myImageView setImage: updatedImg];
}

I hope this help you ;)

thx for vote ;)

BlackSheep
  • 1,087
  • 12
  • 29
0

This question appears to address your needs:

Add Text to UIImage

Perhaps ignore the camera action, and the camera roll, what you really just want to do is add a string to a UIImage.

Community
  • 1
  • 1
Tim
  • 8,932
  • 4
  • 43
  • 64
  • That answer led me to http://stackoverflow.com/questions/6992830/how-to-write-text-on-image-in-objective-c-iphone which is did the trick. Thanks – JSA986 Jun 29 '13 at 14:08