手机截屏

你是否想对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];
    }];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值