how to take a screenshot of the iPhone programmatically?

前端 未结 11 1836
谎友^
谎友^ 2020-11-28 08:01

Is it possible in objective C that we can take the screen shot of screen and stored this image in UIImage.

相关标签:
11条回答
  • 2020-11-28 08:06

    Yes, here's a link to the Apple Developer Forums https://devforums.apple.com/message/149553

    0 讨论(0)
  • 2020-11-28 08:11

    You need to create a bitmap context of the size of your screen and use

    [self.view.layer renderInContext:c]
    

    to copy your view in it. Once this is done, you can use

    CGBitmapContextCreateImage(c)
    

    to create a CGImage from your context.

    Elaboration :

    CGSize screenSize = [[UIScreen mainScreen] applicationFrame].size;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef ctx = CGBitmapContextCreate(nil, screenSize.width, screenSize.height, 8, 4*(int)screenSize.width, colorSpaceRef, kCGImageAlphaPremultipliedLast);
    CGContextTranslateCTM(ctx, 0.0, screenSize.height);
    CGContextScaleCTM(ctx, 1.0, -1.0);
    
    [(CALayer*)self.view.layer renderInContext:ctx];
    
    CGImageRef cgImage = CGBitmapContextCreateImage(ctx);
    UIImage *image = [UIImage imageWithCGImage:cgImage];
    CGImageRelease(cgImage);
    CGContextRelease(ctx);  
    [UIImageJPEGRepresentation(image, 1.0) writeToFile:@"screen.jpg" atomically:NO];
    

    Note that if you run your code in response to a click on a UIButton, your image will shows that button pressed.

    0 讨论(0)
  • 2020-11-28 08:11

    Use this code to take the screenshot:

    -(void)webViewDidFinishLoad:(UIWebView *)webView
    {
    UIGraphicsBeginImageContext(self.webView.bounds.size);
    
        [self.webView.layer renderInContext:UIGraphicsGetCurrentContext()];
    
        UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        UIImageWriteToSavedPhotosAlbum(screenshotImage, nil, nil, nil);
    
        NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    
        NSString *str1=[NSString stringWithFormat:@"%d",myInt];
    
        NSString *pngFilePath = [NSString stringWithFormat:@"%@/page_%@.png",docDir,str1]; // any name u want for image
        NSLog(@"%@",pngFilePath);
        NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(screenshotImage)]; 
        [data1 writeToFile:pngFilePath atomically:YES];
    }
    
    0 讨论(0)
  • 2020-11-28 08:12

    The previous code assumes that the view to be captured lives on the main screen...it might not.

    Would this work to always capture the content of the main window? (warning: compiled in StackOverflow)

    
    - (UIImage *) captureScreen {
        UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
        CGRect rect = [keyWindow bounds];
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [keyWindow.layer renderInContext:context];   
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return img;
    }
    
    0 讨论(0)
  • 2020-11-28 08:12

    try this...

    - (UIImage*)captureView:(UIView *)view 
    {
        CGRect rect = [[UIScreen mainScreen] bounds];
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [view.layer renderInContext:context];
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return img;
    }
    
    - (void)saveScreenshotToPhotosAlbum:(UIView *)view 
    {
        UIImageWriteToSavedPhotosAlbum([self captureView:self.view], nil, nil,nil);
    }
    
    0 讨论(0)
  • 2020-11-28 08:16

    // 100% Work

    - (UIImage *)screenshot
    {                   
      UIGraphicsBeginImageContextWithOptions(self.main_uiview.bounds.size, NO, 2.0f);
      [self.main_uiview drawViewHierarchyInRect:_main_uiview.bounds afterScreenUpdates:YES];
    
      UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
    
      return image;
    }
    
    0 讨论(0)
提交回复
热议问题