Automate Screenshots on iPhone Simulator?

后端 未结 6 1633
耶瑟儿~
耶瑟儿~ 2020-12-23 02:13

I\'m tired of taking new screenshots everytime I change my UI for my iPhone application. I would like to be able to run a script/program/whatever to load my binary on the si

6条回答
  •  时光取名叫无心
    2020-12-23 02:30

    The private UIGetScreenImage(void) API can be used to capture the contents of the screen:

    CGImageRef UIGetScreenImage();
    void SaveScreenImage(NSString *path)
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        CGImageRef cgImage = UIGetScreenImage();
        void *imageBytes = NULL;
        if (cgImage == NULL) {
            CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
            imageBytes = malloc(320 * 480 * 4);
            CGContextRef context = CGBitmapContextCreate(imageBytes, 320, 480, 8, 320 * 4, colorspace, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Big);
            CGColorSpaceRelease(colorspace);
            for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
                CGRect bounds = [window bounds];
                CALayer *layer = [window layer];
                CGContextSaveGState(context);
                if ([layer contentsAreFlipped]) {
                    CGContextTranslateCTM(context, 0.0f, bounds.size.height);
                    CGContextScaleCTM(context, 1.0f, -1.0f);
                }
                [layer renderInContext:(CGContextRef)context];
                CGContextRestoreGState(context);
            }
            cgImage = CGBitmapContextCreateImage(context);
            CGContextRelease(context);
        }
        NSData *pngData = UIImagePNGRepresentation([UIImage imageWithCGImage:cgImage]);
        CGImageRelease(cgImage);
        if (imageBytes)
            free(imageBytes);
        [pngData writeToFile:path atomically:YES];
        [pool release];
    }
    

    Be sure to wrap it inside an #ifdef so it doesn't appear in the release build.

提交回复
热议问题