ios - 截取 tableview 的屏幕截图时如何包含导航栏

标签 ios swift uitableview uinavigationbar screenshot

我想在我的 tableview 屏幕截图图像中包含导航栏。以下代码用于捕获整个 TableView ,我尝试了其他代码来捕获导航栏而不是整个 TableView 。可以同时进行吗?

func screenshot(){
    var image = UIImage();

    UIGraphicsBeginImageContextWithOptions(CGSize(width:tableView.contentSize.width, height:tableView.contentSize.height),false, 0.0)
    let context = UIGraphicsGetCurrentContext()
    let previousFrame = tableView.frame
    tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.contentSize.width, height: tableView.contentSize.height)
    tableView.layer.render(in: context!)
    tableView.frame = previousFrame
    image = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)

}

最佳答案

导航栏和tableview分别截图,然后合并。

objective-c :

-(UIImage*)merge:(UIImage*)tvImage with:(UIImage*)navImage {

    CGFloat contextHeight = tableView.contentSize.height + self.navigationController.navigationBar.frame.size.height;
    CGRect contextFrame = CGRectMake(0, 0, tableView.frame.size.width, contextHeight);

    UIGraphicsBeginImageContextWithOptions(contextFrame.size, NO, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIGraphicsPushContext(context);


    //1 draw navigation image in context
    [navImage drawInRect:self.navigationController.navigationBar.frame];

    //2 draw tableview image in context
    CGFloat y = self.navigationController.navigationBar.frame.size.height;
    CGFloat h = tableView.contentSize.height;
    CGFloat w = tableView.frame.size.width;
    [tvImage drawInRect:CGRectMake(0, y, w, h)];


    // Clean up and get the new image.
    UIGraphicsPopContext();
    UIImage *mergeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return mergeImage;

}

swift 3:

func merge(tvImage:UIImage, with navImage:UIImage) {

        let contextHeight:CGFloat = tableView.contentSize.height + self.navigationController!.navigationBar.frame.size.height;
        let contextFrame:CGRect = CGRect(0, 0, tableView.frame.size.width, contextHeight);

        UIGraphicsBeginImageContextWithOptions(contextFrame.size, false, 0.0);
        let context:CGContext = UIGraphicsGetCurrentContext();
        UIGraphicsPushContext(context);


        //1 draw navigation image in context
        navImage.draw(in: self.navigationController.navigationBar.frame)


        //2 draw tableview image in context
        let y:CGFloat = self.navigationController.navigationBar.frame.size.height;
        let h:CGFloat = tableView.contentSize.height;
        let w:CGFloat = tableView.frame.size.width;
        tvImage.draw(in: CGRectMake(0, y, w, h))



        // Clean up and get the new image.
        UIGraphicsPopContext();
        let mergeImage:UIImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        return mergeImage;

    }

关于ios - 截取 tableview 的屏幕截图时如何包含导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45830346/

相关文章:

swift - iOS13 中 FCM 的推送通知问题

ios - Swift - UITableViewController 和 Storyboard 未链接

ios - 如何为下一个/后一个 UIImageView 创建滑动

android - 我可以检测用户是否在trigger.io上全局禁用推送通知吗?

ios - 在 UITableView 集合上水平滚动

iphone - UITableView:将一行移动到空白部分

android - Cordova /Phonegap : run FileTransfer plugin in background thread

swift - 使用 DispatchGroup 在 for-in 循环中处理异步回调仅在所有循环都成功时有效

iphone - 如何防止 reloadRowsAtIndexPaths 阻止用户输入?

iphone - 如何使用 UITableView 进行垂直分页?