Resolved add timestamp on top of image and save as new image

Discussion in 'iOS Programming' started by straber, Mar 15, 2013.

  1. straber, Mar 15, 2013
    Last edited: Mar 17, 2013

    straber macrumors member

    Jul 3, 2012
    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?

  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Draw the image into an image context then draw the text onto the context then get back a new image from the image context.
  3. straber, Mar 17, 2013
    Last edited by a moderator: Mar 18, 2013

    straber thread starter macrumors member

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

    UIImage *newImg = [self scaleAndRotateImage:image];
    	UIImage *timestamped;
        CGSize imgSize = newImg.size;
        [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();
  4. Duncan C macrumors 6502a

    Duncan C

    Jan 21, 2008
    Northern Virginia

    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.
  5. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    You should also look into using NSDateFormatter's stringFromDate: instance method, rather than building your own date string.

Share This Page