zoukankan      html  css  js  c++  java
  • IOS 截屏(保存到相册中)

    @interface NJViewController ()
    /**
     *  点击截屏按钮
     */
    - (IBAction)captureView:(UIButton *)sender;
    /**
     *  白色view
     */
    @property (weak, nonatomic) IBOutlet UIView *customView;
    
    @end
    
    @implementation NJViewController
    - (IBAction)captureView:(UIButton *)sender {
        
        // 延迟1 ~2 秒之后再截屏
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            // 1.创建一个bitmap的上下文
            UIGraphicsBeginImageContext(self.view.frame.size);
            
            
            // 2.将屏幕绘制到上下文中
    //        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
            
            [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
            
             UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
            
            /*
            // 3.从上下文中取出绘制好的图片
            UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
            
            NSData *data = UIImagePNGRepresentation(newImage);
            
            NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"abc.png"];
            NSLog(@"%@", path);
            [data writeToFile:path atomically:YES];
            */
            
            // 4.保存图片到相册
            
            UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        });
    
    }
    
     - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
    {
    //    NSLog(@"completionImage");
        
        if (error) {
    //        NSLog(@"保存失败");
            [MBProgressHUD showError:@"保存失败, 请检测应用是否拥有访问相册的权限"];
        }else
        {
    //        NSLog(@"保存成功");
            [MBProgressHUD showSuccess:@"保存成功"];
        }
    }
  • 相关阅读:
    [团队项目]典型用户
    0415 操作系统_实验二、作业调度模拟程序
    0415 结对2.0评价
    复利计算- 结对2.0--复利计算WEB升级版
    0408汉堡
    复利计算- 结对1.0
    0405《构建之法》第四章读后感
    复利计算器4.0 【java版】
    复利计算器的单元测试结果
    操作系统 实验一、命令解释程序的编写实验
  • 原文地址:https://www.cnblogs.com/liuwj/p/6598791.html
Copyright © 2011-2022 走看看