zoukankan      html  css  js  c++  java
  • 【代码笔记】iOS-对UIView进行截图

    一,效果图。

    二,工程图。

    三,代码。

    RootViewController.m

    复制代码
    #import "RootViewController.h"
    
    
    @interface RootViewController ()
    
    @end
    
    @implementation RootViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        //UIView
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];
        view.backgroundColor=[UIColor redColor];
        [self.view addSubview:view];
        
        //在UIImageView中显示截取的图片
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(50, 200, 200, 100)];
        imageView.image=[self screenShotView:view];
        [self.view addSubview:imageView];
    }
    #pragma -mark -functions
    // 对指定视图进行截图
    - (UIImage *)screenShotView:(UIView *)view
    {
        UIImage *imageRet = nil;
        
        if (view)
        {
            if(UIGraphicsBeginImageContextWithOptions)
            {
                UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);
            }
            else
            {
                UIGraphicsBeginImageContext(view.frame.size);
            }
            
            //获取图像
            [view.layer renderInContext:UIGraphicsGetCurrentContext()];
            imageRet = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        }else{
        }
        
        return imageRet;
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    复制代码

     

  • 相关阅读:
    Java集合
    插入排序
    修改button的可点击区域
    这就是工作
    Cocos2dx使用TextField实现输入框
    SVN解决本地版本控制与服务器版本冲突问题
    ParallaxNode视差节点实现远景近景的不同层次移动
    人生最重要的三个领域——健康、财富和爱
    什么是开发框架-- (转载)
    C++函数模版的简单使用
  • 原文地址:https://www.cnblogs.com/yang-guang-girl/p/5174427.html
Copyright © 2011-2022 走看看