iOS 截屏方法

时间:2015-08-26 15:35:51   收藏:0   阅读:303

   

static int ScreenshotIndex=0;

    

    //判断是否是retina屏

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]){

        // 开启图像上下文

        UIGraphicsBeginImageContextWithOptions(self.view.window.bounds.size, NO, [UIScreen mainScreen].scale);

    } else {

        // 开启图像上下文

        UIGraphicsBeginImageContext(self.view.window.bounds.size);

    }

    

    // 将当前视图图层渲染到当前上下文

    [self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];

    // 从当前上下文获取图像

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    // 关闭图像上下文

    UIGraphicsEndImageContext();

    // 保存图片到相册

    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

    

    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString * documentsDirectory = [paths objectAtIndex:0];

    NSString * pictureName= [NSString stringWithFormat:@"image_%d.png",ScreenshotIndex];

    self.savedImagePath = [documentsDirectory stringByAppendingPathComponent:pictureName];

    NSLog(@"截屏路径打印: %@", self.savedImagePath);

    

    NSData * data = UIImagePNGRepresentation(image);

    //保存照片到沙盒目录

    [data writeToFile:self.savedImagePath atomically:YES];

 

    ScreenshotIndex++;

原文:http://www.cnblogs.com/tongyuling/p/4760336.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!