0
我有一個滾動視圖,我需要該滾動視圖的完整內容的屏幕截圖,我搜索並嘗試了很多解決方案,但只提供了viewcontroller總高度的屏幕截圖。 例如,如果scrollview的高度是1096而Viewcontroller的高度是532,它會根據Viewcontroller的高度截圖內容 我發現只有這個代碼對於stackoverflow很有用,並且它返回了scrollview的完整高度,但它清除了內容Viewcontroller中的滾動視圖。任何幫助如何保留scrollview的內容或任何更好的解決方案,以迅速採取一個完整的截圖。三江源滾動視圖的內容在屏幕截圖時清除
func getImageOfScrollView()->UIImage{
var image = UIImage();
UIGraphicsBeginImageContextWithOptions(self.scrollView.contentSize, false, UIScreen.mainScreen().scale)
// save initial values
let savedContentOffset = self.scrollView.contentOffset;
let savedFrame = self.scrollView.frame;
let savedBackgroundColor = self.scrollView.backgroundColor
// reset offset to top left point
self.scrollView.contentOffset = CGPointZero;
// set frame to content size
self.scrollView.frame = CGRectMake(0, 0, self.scrollView.contentSize.width, self.scrollView.contentSize.height);
// remove background
self.scrollView.backgroundColor = UIColor.clearColor()
// make temp view with scroll view content size
// a workaround for issue when image on ipad was drawn incorrectly
let tempView = UIView(frame: CGRectMake(0, 0, self.scrollView.contentSize.width, self.scrollView.contentSize.height))
// save superview
let tempSuperView = self.scrollView.superview
// remove scrollView from old superview
self.scrollView.removeFromSuperview()
// and add to tempView
tempView.addSubview(self.scrollView)
// render view
// drawViewHierarchyInRect not working correctly
tempView.layer.renderInContext(UIGraphicsGetCurrentContext())
// and get image
image = UIGraphicsGetImageFromCurrentImageContext();
// and return everything back
tempView.subviews[0].removeFromSuperview()
tempSuperView?.addSubview(self.scrollView)
// restore saved settings
self.scrollView.contentOffset = savedContentOffset;
self.scrollView.frame = savedFrame;
self.scrollView.backgroundColor = savedBackgroundColor
UIGraphicsEndImageContext();
return image
}
謝謝你的努力。 – fmas
That works .. :) – fmas
爲scrollview擴展bounds.size對我來說是最好的。 – Senry