CGImageRef UIGetScreenImage();

- (UIImage *) getScreenImage:(UIView *)shotView{

    CGImageRef cgImage = UIGetScreenImage();

    void *imageBytes = NULL;

    if (cgImage == NULL) {

        CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();

        imageBytes = malloc(shotView.bounds.size.width * shotView.bounds.size.height * 4);

        CGContextRef context = CGBitmapContextCreate(imageBytes, shotView.bounds.size.width, shotView.bounds.size.height, 8, shotView.bounds.size.width * 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);

    }

    UIImage *resultingImage = [UIImage imageWithCGImage:cgImage];

    CGImageRelease(cgImage);

    return resultingImage;

}

 

相关文章:

  • 2021-04-16
  • 2022-12-23
  • 2022-12-23
  • 2022-01-20
  • 2022-02-09
  • 2021-07-20
  • 2021-05-19
  • 2021-11-03
猜你喜欢
  • 2021-10-22
  • 2021-11-13
  • 2021-04-02
  • 2022-12-23
  • 2021-09-27
  • 2021-11-01
相关资源
相似解决方案