zoukankan      html  css  js  c++  java
  • iOS 学习笔记一【屏幕截图,并显示当前View】

    // 直接上代码:

    //
    
    //  ViewController.h
    
    //  屏幕截图测试
    
    //
    
    //  Created by 博爱之家 on 15/11/11.
    
    //  Copyright © 2015年 博爱之家. All rights reserved.
    
    //
    
     
    
    #import <UIKit/UIKit.h>
    
     
    
    @interface ViewController : UIViewController
    
    {
    
        NSData *imageData;
    
    }
    
     
    
    @end

    *************************************

    //
    
    //  ViewController.m
    
    //  屏幕截图测试
    
    //
    
    //  Created by 博爱之家 on 15/11/11.
    
    //  Copyright © 2015年 博爱之家. All rights reserved.
    
    //
    
     
    
    #import "ViewController.h"
    
     
    
    //宏定义
    
    //当前设备的屏幕宽度
    
    #define KSCREEN_WIDTH   [[UIScreen mainScreen] bounds].size.width
    
     
    
    //当前设备的屏幕高度
    
    #define KSCREEN_HEIGHT   [[UIScreen mainScreen] bounds].size.height
    
     
    
    @interface ViewController ()
    
     
    
    @property (nonatomic, strong) UILabel *testlabel;
    
    @property (nonatomic, strong) UIButton *testButton;
    
     
    
    @end
    
     
    
    @implementation ViewController
    
     
    
    - (void)viewDidLoad {
    
        [super viewDidLoad];
    
        // Do any additional setup after loading the view, typically from a nib.
    
        
    
        self.title = @"截屏测试";
    
        self.view.backgroundColor = [UIColor whiteColor];
    
        
    
        self.testlabel = [UILabel new];
    
        self.testlabel.frame = CGRectMake((KSCREEN_WIDTH-200)/2 , 100, 200, 50);
    
        self.testlabel.text = @"截屏测试";
    
     
    
        
    
        self.testButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
        self.testButton.frame = CGRectMake((KSCREEN_WIDTH-200)/2 , 200, 200, 50);
    
        [self.testButton setTitle:@"点击截屏" forState:UIControlStateNormal];
    
        self.testButton.backgroundColor = [UIColor purpleColor];
    
        [self.testButton addTarget:self action:@selector(clickBUutton:) forControlEvents:UIControlEventTouchUpInside];
    
        
    
        [self.view addSubview:self.testlabel];
    
        [self.view addSubview:self.testButton];
    
    }
    
     
    
    - (IBAction)clickBUutton:(id)sender
    
    {
    
        NSLog(@"开始");
    
        [self saveScreenShotsView];
    
        
    
        UIImageView *imageView = [[UIImageView alloc] init];
    
        imageView.frame = CGRectMake(100, 400, 200, 200);
    
        imageView.backgroundColor = [UIColor greenColor];
    
        imageView.image = [UIImage imageWithData:imageData];
    
        
    
        [self.view addSubview:imageView];
    
    }
    
     
    
    // 保存图片
    
    - (void)saveScreenShotsView
    
    {
    
        UIImage *image = [self getNormalImage:self.view];
    
        UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
    
        
    
        [self saveToDisk:image];
    
        NSLog(@"结束");
    
    }
    
     
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    
    {
    
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    
    }
    
     
    
    #pragma mark - 获取屏幕截图
    
    - (UIImage *)getNormalImage:(UIView *)view
    
    {
    
        UIGraphicsBeginImageContext(CGSizeMake(KSCREEN_WIDTH, KSCREEN_HEIGHT));
    
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        [view.layer renderInContext:context];
    
        
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        return image;
    
    }
    
     
    
    #pragma mark - 保存到硬盘中
    
    - (void)saveToDisk:(UIImage *)image
    
    {
    
        NSString *dirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
        
    
        NSLog(@"保存路径: %@", dirPath);
    
        
    
        NSString *path = [NSString stringWithFormat:@"%@/pic_%f.png",dirPath,[NSDate timeIntervalSinceReferenceDate]];
    
        
    
        imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
    
        
    
        [imageData writeToFile:path atomically:YES];
    
        
    
        NSLog(@"保存路径: %@", path);
    
        
    
        NSString *imagePath = [[path componentsSeparatedByString:@"/"] lastObject];
    
        
    
        NSLog(@"保存路径2imagePath: %@", imagePath);
    
        NSLog(@"保存完毕");
    
    }
    
    @end
    
     
  • 相关阅读:
    EF里如何定制实体的验证规则和实现IObjectWithState接口进行验证以及多个实体的同时验证
    EF里查看/修改实体的当前值、原始值和数据库值以及重写SaveChanges方法记录实体状态
    使用EF自带的EntityState枚举和自定义枚举实现单个和多个实体的增删改查
    EF里单个实体的增查改删以及主从表关联数据的各种增删改查
    EF和MVC系列文章导航:EF Code First、DbContext、MVC
    EF如何操作内存中的数据以及加载相关联表的数据:延迟加载、贪婪加载、显示加载
    EF里的继承映射关系TPH、TPT和TPC的讲解以及一些具体的例子
    Git敏捷开发--rebase命令
    浏览器远程编写python代码--jupyter web server
    C++学习--编译优化
  • 原文地址:https://www.cnblogs.com/boai/p/4959960.html
Copyright © 2011-2022 走看看