我想在我的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;
}
答案 1 :(得分:0)
编写扩展将始终帮助我们重用。我在下面创建了一个简单的 UIView 扩展检查
extension UIView {
// render the view within the view's bounds, then capture it as image
func asImage() -> UIImage {
let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image(actions: { rendererContext in
layer.render(in: rendererContext.cgContext)
})
}
}
用法:
self.imageview.image = self.view.asImage() // If you want to capture the View
self.imageview.image = self.tabBarController?.view.asImage() // If it's TabbarViewController