iOS捕捉截屏事件并展示截圖效果
摩拜單車、微信的截屏就做的比較人性化。
現(xiàn)在很多APP開始支持用戶截屏后,主動(dòng)獲取截圖并彈出分享視圖,這樣用戶就不用去相冊去找了,感覺體驗(yàn)不錯(cuò),今天就分享一下 截屏開發(fā)的心得,希望能幫助iOS的朋友。
iOS7之后,蘋果開放出一個(gè)通知:UIApplicationUserDidTakeScreenshotNotification,截屏?xí)r系統(tǒng)就會(huì)發(fā)出這個(gè)通知,需要你注冊這個(gè)通知,就能捕捉到截屏圖片。
下面的代碼,實(shí)現(xiàn)的是用戶截屏后,捕獲到截屏圖片,展示出來:
//注冊截屏通知
[[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); }
按鈕點(diǎn)擊事件:
-(void)shareBtn:(UIButton *)sender{ /* 分享代碼 */ }
以上就是截屏后的事例代碼,最后附上效果圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS使用pageViewController實(shí)現(xiàn)多視圖滑動(dòng)切換
這篇文章主要為大家詳細(xì)介紹了iOS使用pageViewController實(shí)現(xiàn)多視圖滑動(dòng)切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06iOS開發(fā)技巧之WeakSelf宏的進(jìn)化詳解
在程序中我們經(jīng)常用到Block,但寫weak self 時(shí)會(huì)比較繁瑣,下面這篇文章主要給大家介紹了關(guān)于iOS開發(fā)技巧之WeakSelf宏的進(jìn)化的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們一起來看看吧2018-05-05iOS使用 CABasicAnimation 實(shí)現(xiàn)簡單的跑馬燈(無cpu暴漲)
本篇文章主要介紹了iOS使用 CABasicAnimation 實(shí)現(xiàn)簡單的跑馬燈(無cpu暴漲),具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01react-native中AsyncStorage實(shí)例詳解
這篇文章主要介紹了react-native中AsyncStorage實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03iOS App開發(fā)中的UIPageControl分頁控件使用小結(jié)
UIPageControl分頁控件的例子簡單來說即是我們平時(shí)翻動(dòng)多個(gè)桌面頁時(shí)及底部帶有的圓點(diǎn)頁碼標(biāo)注,這里我們來看一下iOS App開發(fā)中的UIPageControl分頁控件使用小結(jié),需要的朋友可以參考下2016-06-06iOS 對plist文件進(jìn)行讀寫,增刪改查的實(shí)例
下面小編就為大家?guī)硪黄猧OS 對plist文件進(jìn)行讀寫,增刪改查的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02安裝win10+黑蘋果雙系統(tǒng)零基礎(chǔ)教程(圖文)
這篇文章主要介紹了安裝win10+黑蘋果雙系統(tǒng)零基礎(chǔ)教程(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2020-01-01