2

嗨,我正在开发一个 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);

//--------------------------------------------------------------------------------------//
4

1 回答 1

2

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

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

MM代表月份和mm分钟。

于 2012-11-28T08:06:41.427 回答