溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

iOS如何實現(xiàn)捕捉截屏事件并展示截圖效果

發(fā)布時間:2021-07-21 14:40:54 來源:億速云 閱讀:197 作者:小新 欄目:移動開發(fā)

小編給大家分享一下iOS如何實現(xiàn)捕捉截屏事件并展示截圖效果,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

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

iOS7之后,蘋果開放出一個通知:UIApplicationUserDidTakeScreenshotNotification,截屏?xí)r系統(tǒng)就會發(fā)出這個通知,需要你注冊這個通知,就能捕捉到截屏圖片。

下面的代碼,實現(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);
}

按鈕點擊事件:

-(void)shareBtn:(UIButton *)sender{

/*
 分享代碼 
*/
}

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

iOS如何實現(xiàn)捕捉截屏事件并展示截圖效果

以上是“iOS如何實現(xiàn)捕捉截屏事件并展示截圖效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI