Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the screenshot function of iOS

Shulou Source: shulou.com Published: 2022-06-03 09:30:42 11月15日 Update

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!

Tags: Screenshots functions pictures content more photos articles good practical ordinary success code area size mobile phone article photo gallery interface directory look Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Linux Shulou Technology vpn Redmi