iOS长截图

- (void)saveBtn:(UIButton *)sender{
    
  
    UIImage *saveImage = [self getTableViewImageWithTableView:self.tableView];
    
    
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        
        if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied) {
            
            UIAlertController *openPH = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"无访问相册权限,是否去打开权限" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *suerAction = [UIAlertAction actionWithTitle:@"打开" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                if ([[UIApplication sharedApplication] canOpenURL:url]) {
                    [[UIApplication sharedApplication] openURL:url];
                }
            }];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [openPH addAction:suerAction];
            [openPH addAction:cancelAction];
            [self presentViewController:openPH animated:YES completion:nil];
            
        }else {
            
            if(saveImage){
                //方法一:保存到系统相册
                UIImageWriteToSavedPhotosAlbum(saveImage, self, @selector((image:didFinishSavingWithError:contextInfo:)), (__bridge void *)self);
                
                //方法二:保存到自定义的appname的相册
                //                NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
                //                [self saveImageToAlbumImage:self.togetherImage albumName:appName];
                
            }
            
        }
    }];
    

    
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        
        [MBProgressHUD showHUDByTitle:@"保存相册失败,请重新尝试" andErrorCode:0 customView:YES addTo:self.view];
    }else {
        
        [MBProgressHUD showHUDByTitle:@"成功保存到相册" andErrorCode:0 customView:YES addTo:self.view];
    }
}


- (UIImage *)getTableViewImageWithTableView:(UITableView *)tableView{
    
    UIImage *viewImage = nil;
    UITableView *scrollView = tableView;
//    NSLog(@"contentsize.height-%f,opaque-%d,scale-%f",scrollView.contentSize.height,scrollView.opaque,[[UIScreen mainScreen] scale]);
    UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, scrollView.opaque, [[UIScreen mainScreen] scale]);
    {
        CGPoint saveContentOffSet = scrollView.contentOffset;
        CGRect savedFrame = scrollView.frame;
        
        scrollView.contentOffset = CGPointZero;
        scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
//        NSLog(@"scrollView.frame.height-%f",scrollView.contentSize.height);

        [scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
        viewImage = UIGraphicsGetImageFromCurrentImageContext();
        
        scrollView.contentOffset = saveContentOffSet;
        scrollView.frame = savedFrame;
    
    }
    
    UIGraphicsEndImageContext();
    
    return viewImage;
    
}

你可能感兴趣的:(iOS长截图)