简体   繁体   English

如何以编程方式拍摄屏幕快照以及当前打印的日期?

[英]How to take screenshot programmatically along with current date printed on it?

I have taken screenshot programmatically for a scroll view in xcode. 我已经以编程方式为xcode中的滚动视图拍摄了屏幕截图。 But the problem i am facing is, i need to take screenshot along with present date in it. 但是我面临的问题是,我需要在屏幕快照中加上当前日期。 Please help me. 请帮我。

截屏之前,请在标签上粘贴当前日期,并在截屏后将其删除。

This is how you can add a text to an image. 这样可以在图像中添加文本。 Try this with the date as string. 尝试使用日期作为字符串。 Once you taken the screenshot pass it to the below method and use the output image. 截取屏幕截图后,将其传递给以下方法并使用输出图像。 Modify the below method for your convenience. 为方便起见,修改以下方法。

//Add text to UIImage
-(UIImage *)addText:(UIImage *)img text:(NSString *)text1{
    int w = img.size.width;
    int h = img.size.height; 
    //lon = h - lon;
    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];// "05/05/09";
    CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextSetRGBFillColor(context, 255, 255, 255, 1);


    //rotate text
    CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 ));

    CGContextShowTextAtPoint(context, 4, 52, text, strlen(text));

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

    return [UIImage imageWithCGImage:imageMasked];
}

Source 资源

Or you could use the EXIF meta data to record the date (it's probably already in there). 或者,您可以使用EXIF元数据来记录日期(日期可能已经在其中)。 I guess an embedded date in the image is harder to fake, though. 我想图像中的嵌入日期很难伪造。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM