How Do I Take a Screen Shot of a UIView?

后端 未结 15 2573
-上瘾入骨i
-上瘾入骨i 2020-11-22 09:28

I am wondering how my iPhone app can take a screen shot of a specific UIView as a UIImage.

I tried this code but all I get is a blank image

相关标签:
15条回答
  • 2020-11-22 09:52

    The following snippet is used to take screenshot :

    UIGraphicsBeginImageContext(self.muUIView.bounds.size);
    
    [myUIView.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    Use renderInContext: method instead of drawInContext: method

    renderInContext: method renders the receiver and its sublayers into current context. This method renders directly from the layer tree.

    0 讨论(0)
  • 2020-11-22 09:52

    you can use following UIView category -

    @implementation UIView (SnapShot)
    
     - (UIImage *)snapshotImage
    {
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);        
        [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];        
        // old style [self.layer renderInContext:UIGraphicsGetCurrentContext()];        
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();        
        UIGraphicsEndImageContext();        
        return image;
    }    
    @end
    
    0 讨论(0)
  • 2020-11-22 09:54

    Apple does not allow:

    CGImageRef UIGetScreenImage();

    Applications should take a screenshot using the drawRect method as specified in: http://developer.apple.com/library/ios/#qa/qa2010/qa1703.html

    0 讨论(0)
提交回复
热议问题