码迷,mamicode.com
首页 > 其他好文 > 详细

截取屏幕并保存到本地

时间:2015-07-08 12:20:35      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

 /* Capture the screenshot */
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, 0.0f);

    if ([self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES]){
        NSLog(@"Successfully draw the screenshot.");
    } else {
        NSLog(@"Failed to draw the screenshot.");
    }
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    /* Save it to disk */
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSURL *documentsFolder = [fileManager URLForDirectory:NSDocumentDirectory
                                                 inDomain:NSUserDomainMask
                                        appropriateForURL:nil
                                                   create:YES
                                                    error:nil];
    NSURL *screenshotUrl = [documentsFolder
                           URLByAppendingPathComponent:@"screenshot.png"];
    
    NSData *screenshotData = UIImagePNGRepresentation(screenshot);
    
    if ([screenshotData writeToURL:screenshotUrl atomically:YES]){
        NSLog(@"Successfully saved screenshot to %@", screenshotUrl);
    } else {
        NSLog(@"Failed to save screenshot.");
    }

截取屏幕并保存到本地

标签:

原文地址:http://www.cnblogs.com/NSNULL/p/4629556.html

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