你是否想对app的特定页面进行截屏分享出去或保存起来呢!
现在以把对app进行全屏截图保存到系统目录为例子:
截屏处理,返回截屏的图像:
-(UIImage*)cutOffScreen { UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow]; UIGraphicsBeginImageContextWithOptions(screenWindow.frame.size, NO, 0.0); // no ritina CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context); for (UIWindow *window in [[UIApplication sharedApplication] windows]) { if(window == screenWindow) { break; }else{ [window.layer renderInContext:context]; } } if ([screenWindow respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) { [screenWindow drawViewHierarchyInRect:screenWindow.bounds afterScreenUpdates:YES]; } else { [screenWindow.layer renderInContext:context]; } CGContextRestoreGState(context); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); screenWindow.layer.contents = nil; UIGraphicsEndImageContext(); return image; }
简单的存入照片库。要注意:首先导入这个库QuartzCore.framework,其次在系统Info.plist文件中增加Privacy - Camera Usage Description这个键值对。设置会崩溃的吆!
-(void) screenShotAction { //写入相册 UIImageWriteToSavedPhotosAlbum([self cutOffScreen], self, nil, nil); }
当然你想更完美的调用,你截屏通常有按钮吧,甚至有导航栏,一般在地图页面,那么截屏时最好把它们都隐藏,截屏完毕再恢复。就需要有延迟处理。本例子可以做参照,具体根据实际情况而定。
-(void)processRightBtnHit { NSArray* pAnnotations = [NSArray arrayWithArray:_mapView.annotations]; [_mapView removeAnnotations:pAnnotations];//移除标注 NSArray* pOverlays = [NSArray arrayWithArray:_mapView.overlays]; [_mapView removeOverlays:pOverlays];//移除覆盖层 _mapView.frame = CGRectMake(0, 0, FULL_WIDTH, FULL_HEIGHT); self.navigationBar.hidden = YES; [self handleMidPoints:self.currentPathLocationDataArray]; [UIView animateWithDuration:0.1 animations:^{ } completion:^(BOOL finished) { [self screenShotAction]; }]; } -(void) screenShotAction { //写入相册 UIImageWriteToSavedPhotosAlbum([self cutOffScreen], self, nil, nil); [UIView animateWithDuration:0.1 animations:^{ } completion:^(BOOL finished) { NSArray* pAnnotations = [NSArray arrayWithArray:_mapView.annotations]; [_mapView removeAnnotations:pAnnotations];//移除标注 NSArray* pOverlays = [NSArray arrayWithArray:_mapView.overlays]; [_mapView removeOverlays:pOverlays];//移除覆盖层 _mapView.frame = CGRectMake(0, NAVBAR_HEIGHT, FULL_WIDTH, FULL_HEIGHT - NAVBAR_HEIGHT); self.navigationBar.hidden = NO; [self handleMidPoints:self.currentPathLocationDataArray]; }]; }