国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS捕捉截屏事件并展示截圖效果

2020-07-26 02:21:38
字體:
來源:轉載
供稿:網友

摩拜單車、微信的截屏就做的比較人性化。

現在很多APP開始支持用戶截屏后,主動獲取截圖并彈出分享視圖,這樣用戶就不用去相冊去找了,感覺體驗不錯,今天就分享一下 截屏開發的心得,希望能幫助iOS的朋友。

iOS7之后,蘋果開放出一個通知:UIApplicationUserDidTakeScreenshotNotification,截屏時系統就會發出這個通知,需要你注冊這個通知,就能捕捉到截屏圖片。

下面的代碼,實現的是用戶截屏后,捕獲到截屏圖片,展示出來:

//注冊截屏通知

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

截屏后捕捉到事件:

- (void)getScreenshot:(NSNotification *)notification{ NSLog(@"捕捉截屏事件"); //獲取截屏圖片 UIImage *image = [UIImage imageWithData:[self imageDataScreenShot]]; //顯示圖片 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:@"分享給好友" 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); }]; //3秒后消失 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{  [backView removeFromSuperview]; });}

獲取截屏圖片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);}

按鈕點擊事件:

-(void)shareBtn:(UIButton *)sender{/* 分享代碼 */}

以上就是截屏后的事例代碼,最后附上效果圖:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丹阳市| 昌乐县| 济宁市| 兴城市| 扶余县| 武强县| 安国市| 衡山县| 邓州市| 凤山市| 阿拉善左旗| 济阳县| 育儿| 四会市| 美姑县| 满洲里市| 安义县| 襄樊市| 建阳市| 福清市| 靖宇县| 双江| 湘潭市| 呼伦贝尔市| 迁西县| 湘乡市| 八宿县| 泗阳县| 通江县| 钦州市| 伊金霍洛旗| 修文县| 新龙县| 千阳县| 哈巴河县| 凉山| 上林县| 诏安县| 永宁县| 宁远县| 井陉县|