码迷,mamicode.com
首页 > 移动开发 > 详细

iOS截屏保存至相册

时间:2016-08-24 19:10:28      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:

#pragma mark 截屏并保存至相册

-(void)screenShotsComplete:(void(^)(UIImage * img)) complete

{

    CGSize imageSize = [[UIScreen mainScreen] bounds].size;

    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);

    

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    for (UIWindow * window in [[UIApplication sharedApplication] windows]) {

        if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) {

            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);

            [[window layer] renderInContext:context];

            

            CGContextRestoreGState(context);

        }

    }

    

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);

    NSLog(@"Suceeded!");

    if(complete){

        complete(image);

    }

 

}

 

//点击保存至相册后,出现动画效果

[self screenShotsComplete:^(UIImage *img) {

            UIImageView * imgView=[[UIImageView alloc]initWithFrame:weakSelf.view.bounds];

            imgView.image=img;

            [weakSelf.view addSubview:imgView];

            

            CGPoint center=CGPointMake(weakSelf.view.center.x, weakSelf.view.center.y-64);

            [UIImageView animateWithDuration:1.0f animations:^{

                imgView.width-=imgView.width;

                imgView.height-=imgView.height;

                imgView.alpha=0.0f;

                imgView.center=center;

            } completion:^(BOOL finished) {

                [imgView removeFromSuperview];

                btn.enabled=YES;

                [HintInfoView showOnView:weakSelf.view hintTitle:@"保存成功!" hintFont:weakSelf.commonTitleFont];

            }];

            

            

        }];

    };

 

 

 

 

 

iOS截屏保存至相册

标签:

原文地址:http://www.cnblogs.com/daxueshan/p/5804060.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!