码迷,mamicode.com
首页 > 移动开发 > 详细

iOS-截图和把截图封装成一个方法

时间:2015-09-10 12:38:35      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

//
//  UIImage+Tools.h
//  截屏
//
//  Created by YaguangZhu on 15/9/10.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIImage (Tools)
+(instancetype)imageWithCaptureView:(UIView *)view;

@end



//
//  UIImage+Tools.m
//  截屏
//
//  Created by YaguangZhu on 15/9/10.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "UIImage+Tools.h"

@implementation UIImage (Tools)

+ (instancetype)imageWithCaptureView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:ctx];
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return newImage;
}
@end
//
//  ViewController.m
//  截屏
//
//  Created by YaguangZhu on 15/9/10.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "ViewController.h"
#import "UIImage+Tools.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIImage *newImage = [UIImage imageWithCaptureView:self.view];
    NSData *data = UIImagePNGRepresentation(newImage);
    
    [data writeToFile:@"/Users/yaguangzhu/Desktop/00d1.png" atomically:YES];

    
}
- (void)CaptureView
{
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:ctx];
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    NSData *data = UIImagePNGRepresentation(newImage);
    
    [data writeToFile:@"/Users/yaguangzhu/Desktop/00d1.png" atomically:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

iOS-截图和把截图封装成一个方法

标签:

原文地址:http://www.cnblogs.com/zhuyaguang/p/4797262.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!