Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the screenshot function of iOS

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you about how iOS implements screenshots. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Common interface

/ * screenshot function * /-(void) screenShot {UIGraphicsBeginImageContextWithOptions (CGSizeMake (640,960), YES, 0); / / set the screenshot size [[self.view layer] renderInContext:UIGraphicsGetCurrentContext ()]; UIImage * viewImage = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); CGImageRef imageRef = viewImage.CGImage; CGRect rect = CGRectMake (0,0,641, SCREEN_HEIGHT + 300); / / you can set the area where you want to take screenshots CGImageRef imageRefRect = CGImageCreateWithImageInRect (imageRef, rect) UIImage * sendImage = [[UIImage alloc] initWithCGImage:imageRefRect]; / / the following is the image saving code UIImageWriteToSavedPhotosAlbum (sendImage, nil, nil, nil); / / Save the picture to the photo gallery NSData * imageViewData = UIImagePNGRepresentation (sendImage); NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsDirectory = [paths objectAtIndex:0]; NSString * pictureName= @ "screenShow.png"; NSString * savedImagePath = [documentsDirectory stringByAppendingPathComponent:pictureName]; [imageViewData writeToFile:savedImagePath atomically:YES] / / Save photos to sandboxie directory CGImageRelease (imageRefRect); / / load pictures UIImage * bgImage2 = [[UIImage alloc] initWithContentsOfFile:savedImagePath] from the phone locally;}

2.UIScrollView screenshot (one screen cannot be fully displayed)

/ * Screenshot * /-(void) screenShot {UIImage* image = nil; UIGraphicsBeginImageContext (m_scrollView.contentSize); {CGPoint savedContentOffset = massively scrollView.contentOffset; CGRect savedFrame = massively scrollView.frame; m_scrollView.contentOffset = CGPointZero; m_scrollView.frame = CGRectMake (0,0, m_scrollView.contentSize.width, m_scrollView.contentSize.height); [m_scrollView.layer renderInContext: UIGraphicsGetCurrentContext ()] Image = UIGraphicsGetImageFromCurrentImageContext (); m_scrollView.contentOffset = savedContentOffset; m_scrollView.frame = savedFrame;} UIGraphicsEndImageContext (); if (image! = nil) {NSLog (@ "Screenshot succeeded!");}} Thank you for reading! This is the end of the article on "how to achieve screenshot 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report