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 capture the screenshot event and show the screenshot effect by iOS

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "how iOS can capture screenshots and show the screenshot effect". The content is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn the article "how to capture screenshots and show screenshots by iOS".

After iOS7, Apple opened a notice: UIApplicationUserDidTakeScreenshotNotification, the system will send this notice when the screenshot, you need to register this notice, you can capture the screenshot.

The following code implements that after taking a screenshot, the user captures the screenshot and displays it:

/ / register for screenshot notification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (getScreenShot:) name:UIApplicationUserDidTakeScreenshotNotification object:nil]

Events are captured after screenshots:

-(void) getScreenshot: (NSNotification *) notification {NSLog (@ "capture screenshot event"); / / get a screenshot image UIImage * image = [UIImage imageWithData: [self imageDataScreenShot]]; / / display an image UIImageView * imgV = [[UIImageView alloc] initWithImage:image]; imgV.frame = [UIScreen mainScreen] .bounds; UIView * backView = [UIView alloc] initWithFrame:CGRectMake (0,0, SCREEN_WIDTH, SCREEN_HEIGHT)]; backView.backgroundColor = [UIColor grayColor] colorWithAlphaComponent:0.8] UIButton * shareBtn = [UIButton buttonWithType:UIButtonTypeSystem]; shareBtn.titleLabel.font = [UIFont systemFontOfSize:17.0]; [shareBtn setTintColor: [UIColor whiteColor]]; shareBtn.frame = CGRectMake (SCREEN_WIDTH/5,SCREEN_HEIGHT, SCREEN_WIDTH*3/5,50); [shareBtn.layer setMasksToBounds:YES]; [shareBtn.layer setBorderWidth:1]; shareBtn.layer.cornerRadius = 6; [shareBtn setTitle:@ "share with friends" forState:UIControlStateNormal]; shareBtn.backgroundColor = [SouFunIMUtilityHelper colorWithHexString:@ "# B22222"] [shareBtn addTarget:self action:@selector (shareBtn:) forControlEvents:UIControlEventTouchUpInside]; [backView addSubview:imgV]; [backView addSubview:shareBtn]; UIWindow * window = [UIApplication sharedApplication] .keyWindow; [window addSubview:backView]; [UIView animateWithDuration:1.0 animations: ^ {imgV.transform = CGAffineTransformMakeScale (0.8,0.8); shareBtn.transform = CGAffineTransformMakeTranslation (0,-50);}] / / disappear dispatch_after after 3 seconds (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (3 * NSEC_PER_SEC)), dispatch_get_main_queue (), ^ {[backView removeFromSuperview];});}

Get the screenshot image data:

-(NSData *) imageDataScreenShot {CGSize imageSize = CGSizeZero; imageSize = [UIScreen mainScreen] .bounds.size; UIGraphicsBeginImageContextWithOptions (imageSize, NO, 0); CGContextRef context = UIGraphicsGetCurrentContext (); for (UIWindow * window in [[UIApplication sharedApplication] windows]) {CGContextSaveGState (context); CGContextTranslateCTM (context, window.center.x, window.center.y); CGContextConcatCTM (context, window.transform); CGContextTranslateCTM (context,-window.bounds.size.width * window.layer.anchorPoint.x,-window.bounds.size.height * window.layer.anchorPoint.y) If ([window respondsToSelector:@selector (drawViewHierarchyInRect:afterScreenUpdates:)]) {[window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];} else {[window.layer renderInContext:context];} CGContextRestoreGState (context);} UIImage * image = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); return UIImagePNGRepresentation (image);}

Button click event:

-(void) shareBtn: (UIButton *) sender {/ * sharing code * /}

The above is all the contents of the article "how to capture screenshots and show screenshots by iOS". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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