用Kif截取屏幕截图

时间:2013-05-02 14:52:27

标签: iphone ios screenshot kif-framework

我想在Kif截取屏幕截图。我在kif项目的私有类中看到here是可能的,但是我很难将其转换为步骤。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:3)

这是在KIF 3.0.4中添加的。请参阅here

答案 1 :(得分:2)

稍等一下后,我将其添加到我的KIFTestStep.m

 + (id)stepToTakeScreenShotwithName:(NSString *)name;
 {
     NSString *description = [NSString stringWithFormat:@"Take a screenshot saved by the name %@", name];

     return [self stepWithDescription:description executionBlock:^(KIFTestStep *step, NSError **error) {

         NSString *outputPath = [NSString stringWithFormat:@"/Users/%@/ScreenShots", NSUserName()];

         NSArray *windows = [[UIApplication sharedApplication] windows];
         if (windows.count == 0) {
             return KIFTestStepResultFailure;
         }

         UIGraphicsBeginImageContext([[windows objectAtIndex:0] bounds].size);
         for (UIWindow *window in windows) {
             [window.layer renderInContext:UIGraphicsGetCurrentContext()];
         }
         UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
         UIGraphicsEndImageContext();

         NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
         NSNumber *timeStampObj = [NSNumber numberWithDouble: timeStamp];

         outputPath = [outputPath stringByExpandingTildeInPath];
         outputPath = [outputPath stringByAppendingPathComponent:[name stringByReplacingOccurrencesOfString:@"/" withString:@"_"]];
         outputPath = [outputPath stringByAppendingString:[timeStampObj stringValue]];
         outputPath = [outputPath stringByAppendingPathExtension:@"png"];
         [UIImagePNGRepresentation(image) writeToFile:outputPath atomically:YES];

         return KIFTestStepResultSuccess;
    }];
 }