0

I'm trying to create a screenshot tweak for iOS 7 but all i am able to save is a blank image. This is my code:

UIView *screenshotView = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:NO];
        UIGraphicsBeginImageContext(screenshotView.bounds.size);
        [[screenshotView layer] renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

[UIImageJPEGRepresentation(image, 1.0) writeToFile:fname atomically:YES];

Does anyone have a solution?

thank you

Turnip
  • 35,836
  • 15
  • 89
  • 111
gerula
  • 1

3 Answers3

0

It appears you should not use snapshotViewAfterScreenUpdates to render images (see raywenderlich forum) but drawViewHierarchyInRect instead:

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0f);
[self.view drawViewHierarchyInRect:self.view.frame afterScreenUpdates:NO];
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
jokkedk
  • 2,390
  • 2
  • 17
  • 21
0

for compatibility reason i still haven't used snapshotViewAfterScreenUpdates but the following code works. give it a try.

UIGraphicsBeginImageContextWithOptions(size, myView.opaque, 2.0);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// do something with theImage
Hashmat Khalil
  • 1,826
  • 1
  • 25
  • 49
0

Set the output on a UIImageView

- (UIImage *)screenshot
{
    CGSize imageSize = CGSizeZero;

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (UIInterfaceOrientationIsPortrait(orientation)) {
        imageSize = [UIScreen mainScreen].bounds.size;
    } else {
        imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
    }

    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
        CGContextSaveGState(context);
        CGContextTranslateCTM(context, window.center.x, window.center.y);
        CGContextConcatCTM(context, window.transform);
        CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
        if (orientation == UIInterfaceOrientationLandscapeLeft) {
            CGContextRotateCTM(context, M_PI_2);
            CGContextTranslateCTM(context, 0, -imageSize.width);
        } else if (orientation == UIInterfaceOrientationLandscapeRight) {
            CGContextRotateCTM(context, -M_PI_2);
            CGContextTranslateCTM(context, -imageSize.height, 0);
        } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
            CGContextRotateCTM(context, M_PI);
            CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
        }
        if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
            [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
        } else {
            [window.layer renderInContext:context];
        }
        CGContextRestoreGState(context);
    }

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
hasan
  • 23,815
  • 10
  • 63
  • 101