Fastest way to make screenShot from UIView

I searched a lot but only found two ways to take a screenshot of the UIView. first renderInContext:

I used it on the way

CGContextRef context = [self createBitmapContextOfSize:CGSizeMake(nImageWidth, nImageHeight)];
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, nImageHeight);
CGContextConcatCTM(context, flipVertical);
[self.layer setBackgroundColor:[UIColor clearColor].CGColor];
[self.layer renderInContext:context];
CGImageRef cgImage = CGBitmapContextCreateImage(context);
UIImage* background = [UIImage imageWithCGImage: cgImage];
CGImageRelease(cgImage);

      

The second drawViewHierarchyInRect:

one I used as

UIImage *background = nil;

UIGraphicsBeginImageContextWithOptions (self.bounds.size, NO, self.window.screen.scale);

if ([self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
{
    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];   
}
background = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

      

I know the second is faster than the first and it works for me for the iPhone because the view is small. but when I grab the iPad the video becomes jerky. Can any body tell me a faster way to take a screenshot. any help would be much appreciated

+3


source to share


3 answers


In terms of performance, Apple Docs will report the following:



In addition to -drawViewHierarchyInRect: afterScreenUpdates :, UIView now provides two more snapshot-related methods, -snapshotViewAfterScreenUpdates: and -resizableSnapshotViewFromRect: afterScreenUpdates: withCapInsets :. UIScreen also has -snapshotViewAfterScreenUpdates :.

Unlike the UIView -drawViewHierarchyInRect: afterScreenUpdates :, these methods return a UIView object. If you are looking for a new snapshot review, use one of these methods. This will be more efficient than calling -drawViewHierarchyInRect: afterScreenUpdates: to render the contents of the view to a bitmap . You can use the returned view as a visual stand for the current view / screen in your application. For example, you can use the Snapshot View for animations, where updating the hierarchy of a large view can be costly.

+3


source


There is a third method for taking a snapshot, which is much faster than any of them, but it returns UIView

.

- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates

If you are just using a snapshot to be placed as a background "image" etc ... then I would use it instead.



However, this is only available for iOS8.

It's easy to use ...

UIView *snapshotView = [someView snapshotViewAfterScreenUpdates:YES];

      

+3


source


This method will return you snapshots of a certain kind

-(UIImage *)createSnapShotImagesFromUIview
{
    UIGraphicsBeginImageContext(CGSizeMake(view.frame.size.width,view.frame.size.height));
    CGContextRef context = UIGraphicsGetCurrentContext();
    [mapView.layer renderInContext:context];
    UIImage *img_screenShot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
     return img_screenShot;
}

      

+2


source







All Articles