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

 

 

posted @ 2015-11-12 19:03  博爱1616  阅读(1394)  评论(0编辑  收藏  举报