2017-08-23 50 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