In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to get a screenshot of a view in iOS. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Screenshot in the first case
This is the most common screenshot, for the general view to add views, basically can be used.
Source code:
/ * * normal screenshot this API can only use @ return captured images on views that are not rendered with layer and OpenGL * /-(UIImage *) nomalSnapshotImage {UIGraphicsBeginImageContextWithOptions (self.frame.size, NO, [UIScreen mainScreen] .scale); [self.layer renderInContext:UIGraphicsGetCurrentContext ()]; UIImage * snapshotImage = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); return snapshotImage;}
Screenshot in the second case
If some views are rendered with OpenGL, the screenshot rendered by OpenGL cannot be captured using the above method. In this case, the improved screenshot scheme will be used:
/ * * screenshots of @ return screenshots of views rendered with OpenGL * /-(UIImage *) openglSnapshotImage {CGSize size = self.bounds.size; UIGraphicsBeginImageContextWithOptions (size, NO, [UIScreen mainScreen] .scale); CGRect rect = self.frame; [self drawViewHierarchyInRect:rect afterScreenUpdates:YES]; UIImage * snapshotImage = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); return snapshotImage;}
Screenshot in the third case
After some special Layer (such as AVCaptureVideoPreviewLayer and AVSampleBufferDisplayLayer) are added to a View, none of the above methods can be used to intercept the content on the Layer. You can use an API of the system at this time, but the API can only return one UIView, and the returned UIView can modify parameters such as frame.
/ * * Screenshot returns the converted view * /-(UIView *) snapshotView {UIView * snapView = [self snapshotViewAfterScreenUpdates:YES]; return snapView;} of the image captured by (_ UIReplicantView) @ return in the form of UIView.
Remaining problem: the UIView intercepted by three methods cannot be converted into UIImage. I have tried to write the returned screenshot View to the bitmap and then convert it into UIImage, but the returned UIImage is either empty or has no content. If anyone knows the solution, please let me know.
Screenshot of UIWebView
Last year, when I was doing Bluetooth printing, I tried to convert the content of UIWebView into UIImage. I wrote a category of UIWebView, which can be regarded as a screenshot of UIWebView. By the way, I also posted it.
-(UIImage *) imageForWebView {/ / 1 Get the width and height of WebView CGSize boundsSize = self.bounds.size; CGFloat boundsWidth = boundsSize.width; CGFloat boundsHeight = boundsSize.height; / / 2. Get contentSize CGSize contentSize = self.scrollView.contentSize; CGFloat contentHeight = contentSize.height; / / 3. Save the original offset to make it easy to reset CGPoint offset = self.scrollView.contentOffset; / / 4 after screenshot. Set the initial offset to (0Power0); [self.scrollView setContentOffset:CGPointMake (0L0)]; NSMutableArray * images = [NSMutableArray array]; while (contentHeight > 0) {/ / 5. Get CGContext 5. Get CGContext UIGraphicsBeginImageContextWithOptions (boundsSize, NO, 0. 0); CGContextRef ctx = UIGraphicsGetCurrentContext (); / / 6. Render the area to be intercepted [self.layer renderInContext:ctx]; UIImage * image = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); / / 7. Save the captured images [images addObject:image]; CGFloat offsetY = self.scrollView.contentOffset.y; [self.scrollView setContentOffset:CGPointMake (0, offsetY + boundsHeight)]; contentHeight-= boundsHeight;} / / 8 webView restore to the previous display area [self.scrollView setContentOffset:offset]; CGFloat scale = [UIScreen mainScreen] .scale; CGSize imageSize = CGSizeMake (contentSize.width * scale, contentSize.height * scale); / / 9. Redraw and splice into a complete and clear picture UIGraphicsBeginImageContext (imageSize) according to the resolution of the device; [images enumerateObjectsUsingBlock: ^ (UIImage * image, NSUInteger idx, BOOL * stop) {[image drawInRect:CGRectMake (0 boundsHeight scale * idx,scale * boundsWidth,scale * boundsHeight)]; UIImage * fullImage = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); return fullImage;}
Thank you for reading! This is the end of the article on "how to get a screenshot of a view in iOS". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.