iOS截屏

来源:互联网 发布:java程序的开始 编辑:程序博客网 时间:2024/05/22 12:21
- (void)captureView {            // 1.创建一个bitmap的上下文    UIGraphicsBeginImageContext(self.view.frame.size);            // 2.将屏幕绘制到上下文中    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];        // 3.从上下文中取出绘制好的图片     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();        NSData *data = UIImagePNGRepresentation(newImage);        //保存图片到文件夹中    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"abc.png"];    [data writeToFile:path atomically:YES];        // 4.保存图片到相册    UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);}- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{    if (error) {        NSLog(@"保存失败");    }else    {        NSLog(@"保存成功");    }}


0 0
原创粉丝点击