设为首页 加入收藏

TOP

【代码笔记】iOS-截屏功能
2017-10-13 10:09:55 】 浏览:7348
Tags:代码 笔记 iOS- 功能

代码:

复制代码
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //初始化界面
    [self addView];
    
}
#pragma -mark -functions
//初始化界面
-(void)addView
{
    UIImage *image = [self screenshot:UIDeviceOrientationPortrait
                             isOpaque:YES
                 usePresentationLayer:YES];
    
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)];
    imageView.backgroundColor=[UIColor redColor];
    imageView.image=image;
    [self.view addSubview:imageView];

}
//截屏功能
- (UIImage *)screenshot:(UIDeviceOrientation)orientation isOpaque:(BOOL)isOpaque usePresentationLayer:(BOOL)usePresentationLayer
{
    CGSize size;
    
    if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
        size = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    } else {
        size = CGSizeMake(self.view.frame.size.height, self.view.frame.size.width);
    }
    
    UIGraphicsBeginImageContextWithOptions(size, isOpaque, 0.0);
    
    if (usePresentationLayer) {
        [self.view.layer.presentationLayer renderInContext:UIGraphicsGetCurrentContext()];
    } else {
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    }
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
}
复制代码

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇ios开发随笔第二天 简单动画的实现 下一篇【代码笔记】iOS-截屏功能

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目