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

IOS 截屏单例代码

时间:2014-06-22 21:32:00      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:style   class   code   ext   color   com   

一.  IOS 的截取全屏代码为:
  
    UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
    //1.截屏
    UIGraphicsBeginImageContext(screenWindow.frame.size);
    [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();



二. 截取指定区域:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,self.view.opaque,0.0);
[self.myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*theImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData*theImageData=UIImageJPEGRepresentation(theImage,1.0 );//you can use PNG too
[theImageData writeToFile:@"example.jpeg" atomically:YES];


三.截屏代码的单例
ScreenShot.h文件
#import <Foundation/Foundation.h>

@interface ScreenShot : NSObject
///截屏图片存放位置
@property (nonatomic,copy) NSString *filePath;
/*
 单例函数
 */
+ (id)sharedScreenShot;
/*
 截屏,并写入内存
 */
-(void)getScreenImage;
@end



ScreenShot.m文件

#import "ScreenShot.h"
///截屏图片
#define ScreenShotImage @"screenshot.png"

@implementation ScreenShot
@synthesize filePath=_filePath;



- (id)init {
    if (self = [super init]) {
        _filePath=[self documentsPathForFileName:ScreenShotImage];
    }
    return self;
}
#pragma mark -单例
+ (id)sharedScreenShot {
    static ScreenShot *screenShot = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        screenShot = [[self alloc] init];
    });
    return screenShot;
}

#pragma mark 通过文件名获得路径
- (NSString *)documentsPathForFileName:(NSString *)name
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    
    return [documentsPath stringByAppendingPathComponent:name];
}
#pragma mark - 截屏
-(void)getScreenImage{
    UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
    //1.截屏
    UIGraphicsBeginImageContext(screenWindow.frame.size);
    [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    //2.保存
    NSData *screenshotPNG = UIImagePNGRepresentation(screenshot);
    
    NSError *error = nil;
    //3.写入内存
    [screenshotPNG writeToFile:_filePath options:NSAtomicWrite error:&error];
}
@end



四.使用: 
1.在ViewController中引入头文件ScreenShot.h

    //截屏并保存   [ScreenShot sharedScreenShot] getScreenImage];

2. 获得图片

    NSString *shotFilePath=((ScreenShot *)[ScreenShot sharedScreenShot]).filePath;
    NSData *shotImageData = [NSData dataWithContentsOfFile:shotFilePath];
    UIImage *shotImage = [UIImage imageWithData:shotImageData];




  

IOS 截屏单例代码,布布扣,bubuko.com

IOS 截屏单例代码

标签:style   class   code   ext   color   com   

原文地址:http://blog.csdn.net/wildcatlele/article/details/32160485

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