Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

straber

macrumors member
Original poster
I want to take the UIImage returned when the user takes a picture and write a timestamp in the lower right corner of the image and save the result as a new image. How would I go about doing this?

Thanks...
 
Last edited:
Draw the image into an image context then draw the text onto the context then get back a new image from the image context.

This worked like a charm, thanks! If anyone else is trying to accomplish this, here's my code inside - imagePickerController:didFinishPickingImage:editingInfo:

Code:
UIImage *newImg = [self scaleAndRotateImage:image];
	UIImage *timestamped;
    CGSize imgSize = newImg.size;
    
    UIGraphicsBeginImageContext(imgSize);
    [newImg drawInRect:CGRectMake(0, 0, newImg.size.width, newImg.size.height)];
    
    NSDate *now = [NSDate date];
	NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
	
	//Create the dateformatter object
	NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
	
	//Set the required date format
	[formatter setDateFormat:@"MM/dd/yyyy"];
	
	NSMutableString *month;
	NSMutableString *day;
	NSMutableString *year;
	
	unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
	NSDateComponents *components = [cal components:unitFlags fromDate:now];
	
	month = [NSString stringWithFormat:@"%i", [components month]];
	day = [NSString stringWithFormat:@"%i", [components day]];
	year = [NSString stringWithFormat:@"%i", [components year]];
	
	NSMutableString *dateString = [[NSMutableString alloc] init];
	[dateString appendString:month];
	[dateString appendString:@"/"];
	[dateString appendString:day];
	[dateString appendString:@"/"];
	[dateString appendString:year];
    
    //draw the text
    CGSize theSize;
    theSize = [dateString sizeWithFont:[UIFont systemFontOfSize:44.0f] constrainedToSize:CGSizeMake(newImg.size.width / 2, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
    UIColor *orange = [UIColor colorWithRed:1.0 green:128 / 255.0 blue:0.0 / 255.0 alpha:1.0];
    [orange set];
    CGRect textRect = CGRectMake(newImg.size.width - theSize.width - 20, newImg.size.height - theSize.height - 15, theSize.width, theSize.height);
    [dateString drawInRect:textRect withFont:[UIFont systemFontOfSize:44.0f]];
    
    [dateString release];
    [cal release];
    
    timestamped = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
 
Last edited by a moderator:
This worked like a charm, thanks! If anyone else is trying to accomplish this, here's my code inside - (void)imagePickerController🙁UIImagePickerController *)thePicker didFinishPickingImage🙁UIImage *)image editingInfo🙁NSDictionary *)editingInfo.

Code:
UIImage *newImg = [self scaleAndRotateImage:image];
	UIImage *timestamped;
    CGSize imgSize = newImg.size;
    
    UIGraphicsBeginImageContext(imgSize);
    [newImg drawInRect:CGRectMake(0, 0, newImg.size.width, newImg.size.height)];
    
    NSDate *now = [NSDate date];
	NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
	
	//Create the dateformatter object
	NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
	
	//Set the required date format
	[formatter setDateFormat:@"MM/dd/yyyy"];
	
	NSMutableString *month;
	NSMutableString *day;
	NSMutableString *year;
	
	unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
	NSDateComponents *components = [cal components:unitFlags fromDate:now];
	
	month = [NSString stringWithFormat:@"%i", [components month]];
	day = [NSString stringWithFormat:@"%i", [components day]];
	year = [NSString stringWithFormat:@"%i", [components year]];
	
	NSMutableString *dateString = [[NSMutableString alloc] init];
	[dateString appendString:month];
	[dateString appendString:@"/"];
	[dateString appendString:day];
	[dateString appendString:@"/"];
	[dateString appendString:year];
    
    //draw the text
    CGSize theSize;
    theSize = [dateString sizeWithFont:[UIFont systemFontOfSize:44.0f] constrainedToSize:CGSizeMake(newImg.size.width / 2, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
    UIColor *orange = [UIColor colorWithRed:1.0 green:128 / 255.0 blue:0.0 / 255.0 alpha:1.0];
    [orange set];
    CGRect textRect = CGRectMake(newImg.size.width - theSize.width - 20, newImg.size.height - theSize.height - 15, theSize.width, theSize.height);
    [dateString drawInRect:textRect withFont:[UIFont systemFontOfSize:44.0f]];
    
    [dateString release];
    [cal release];
    
    timestamped = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();


Note that your code to generate a date stamp string is fixed to US date format. You would have been better off to use a date formatter using the short format, and let the date formatter create a date string for you. Then you'd handle European date formats, and even other calendar systems like Chinese, Arabic, Hebrew, etc.
 
You should also look into using NSDateFormatter's stringFromDate: instance method, rather than building your own date string.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.