2017-08-23 28 views
1

我想在我的tableview截图图像中包含导航栏。下面的代码用于捕获整个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) 

} 
+0

把你的窗口截图上,而不是 –

回答

1

分别将导航栏和tableview截屏,然后合并它们。

目标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; 

} 

斯威夫特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; 

    } 
+0

谢谢,但我一直在寻找迅速3.0代码。我是一名新开发人员,不知道Obj-C代码。 –

+0

@ScottTownsend我添加了swift版本,可能会有所帮助。 – Baig

+0

@ScottTownsend是否有效? – Baig