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

    Joined:
    Jul 3, 2012
    #1
    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...
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    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

    Joined:
    Jul 3, 2012
    #3
    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();
     
  4. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #4

    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

    dejo

    Staff Member

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

Share This Page