Friday, 9 December 2011

iOS: Add date And Time on image Or Add text On Imae


- (UIImage *) addText:(UIImage *)img { 
    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); 
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"MMM dd, yyyy HH:mm:ss"];
    NSDate *now = [[NSDate alloc] init];
    NSString *dateString = [format stringFromDate:now];
    [now release];
    [format release];
    char* text = (char *)[dateString cStringUsingEncoding:NSASCIIStringEncoding];
    CGContextSelectFont(context, "Arial", 20, kCGEncodingMacRoman); 
    CGContextSetTextDrawingMode(context, kCGTextFill); 
    CGContextSetRGBFillColor(context, 255,255,255,1); 
    CGContextShowTextAtPoint(context, 24, 52, text, strlen(text)); 
    CGImageRef imageMasked = CGBitmapContextCreateImage(context); 
    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace);
    UIImageWriteToSavedPhotosAlbum([UIImage imageWithCGImage:imageMasked],nil,nil,nil);
    return [UIImage imageWithCGImage:imageMasked]; 

No comments:

Post a Comment