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
    
     
  • 相关阅读:
    结合源码理解Spring MVC处理流程
    Spring Bean的生命周期分析
    面试官:给我说一下你项目中的单点登录是如何实现的?
    Java中的四种引用
    JWT实战总结
    Java读写锁的实现原理
    深入的聊聊Java NIO
    一线大厂Mysql面试题详解
    脱发、秃头防不胜防?这里有一份给码农的减压指南
    手把手教你提高代码Java运行的效率
  • 原文地址:https://www.cnblogs.com/boai/p/4959960.html
Copyright © 2011-2022 走看看