我希望在长时间按下后获得快照,我会让它正常工作。我正在通过以下代码创建快照:
func customSnapShotFrom(view:UIView) -> UIView { // calling this with UITableViewCell input
let snapshot:UIView = view.snapshotViewAfterScreenUpdates(false) // here I tried true and false
snapshot.layer.masksToBounds = false
snapshot.layer.cornerRadius = 0.0
snapshot.layer.shadowOffset = CGSizeMake(2.0, 2.0)
snapshot.layer.shadowOpacity = 1.0
return snapshot
}
它正在运行,但有时我会在输出中收到此消息:
快照未渲染的视图会导致空白 快照。确保您的视图至少在之前呈现过一次 屏幕更新后的快照或快照。
我只为一些细胞(只有少数细胞)而不是总是得到它。有时它会在其返回nil时从该单元格生成快照。我已经检查过了,我总是输入信号。那为什么呢?为什么渲染结果为空快照?感谢
修改 我在手表识别器中添加了手势识别器:
let longPress = UILongPressGestureRecognizer(target: self, action: "longPressDetected:")
self.tableView.addGestureRecognizer(longPress)
我正在longPressDetected
方法创建快照:
func longPressDetected(sender: AnyObject) {
...
switch (state) {
case UIGestureRecognizerState.Began :
...
let cell:UITableViewCell = self.tableView.cellForRowAtIndexPath(indexPath)!
snapshot = self.customSnapShotFrom(cell)
...
我的快速解决方案感谢kirander answer:
func customSnapShotFrom(view:UIView) -> UIView {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0)
view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let cellImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let imageView = UIImageView(image: cellImage)
imageView.layer.masksToBounds = false
imageView.layer.cornerRadius = 0.0
imageView.layer.shadowOffset = CGSizeMake(2.0, 2.0)
imageView.layer.shadowRadius = 4.0
imageView.layer.shadowOpacity = 1.0
return imageView
}
答案 0 :(得分:3)
从here
尝试此代码// make an image from the pressed tableview cell
UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, NO, 0);
[inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *cellImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// create and image view that we will drag around the screen
UIView *snapshot = [[UIImageView alloc] initWithImage:cellImage];