【问题标题】:How to save screenshots with time stamp programatically in iPhone?如何在 iPhone 中以编程方式保存带有时间戳的屏幕截图?
【发布时间】:2012-10-17 11:55:40
【问题描述】:

您好,我正在开发一个 iPad 应用程序,该应用程序需要自动截取屏幕截图,并且图像应该以屏幕名称的名称保存,并带有设备中的当前日期和当前时间。有人帮我做这件事。

我尝试了以下代码。在该代码中截屏,我将在该视图的标签中打印名称。但是在标签中时间不准确,所以我想以时间附加的屏幕名称的名称保存图像。

        //-- Screen Capture --------------------------------------------------------------------//

UIImage *image = nil;

UIGraphicsBeginImageContext(loginBgImgVw.frame.size);

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM-dd-yyyy HH:MM:SS"];
[formatter setTimeZone:[NSTimeZone localTimeZone]];

NSString *dateToday = [formatter stringFromDate:[NSDate date]];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 600, 44)];
[label setText:dateToday];
NSString *fileNameStr = [NSString stringWithFormat:@"%@_Login_%@",providerIdStr,dateToday];

[label setText:fileNameStr];
label.backgroundColor = [UIColor clearColor];
[loginBgImgVw addSubview:label];
[formatter release];


[loginBgImgVw.layer renderInContext: UIGraphicsGetCurrentContext()];     
image = UIGraphicsGetImageFromCurrentImageContext();        
UIGraphicsEndImageContext();        

//=--

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docspath = [paths objectAtIndex:0];
NSLog(@"path=--%@",paths);

NSString *dataPath = [docspath stringByAppendingPathComponent:[NSString stringWithFormat:@"ProviderID_%@",providerIdStr]];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
{
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil];
}

NSString *savedImagePath = [dataPath stringByAppendingPathComponent:@"Login_Provider.png"];
NSData *imageData = UIImageJPEGRepresentation(image, 0.2);
[imageData writeToFile:savedImagePath atomically:NO];

UIImageWriteToSavedPhotosAlbum(image,self,@selector(image:didFinishSavingWithError:contextInfo:),NULL);

//--------------------------------------------------------------------------------------//

【问题讨论】:

  • 我们需要更多信息。你试过什么? “截屏”是指同时按下主页和锁定按钮,还是使用应用内的按钮?
  • 请看我编辑的问题。
  • 正在使用按钮执行此操作。单击提交按钮屏幕截图应捕获。

标签: ios datetime screenshot


【解决方案1】:

您需要将日期格式化程序更改为,

[formatter setDateFormat:@"MM-dd-yyyy HH:mm:ss"];

MM 代表月份,mm 代表分钟。

【讨论】:

    猜你喜欢
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多