获取某些单元格的快照不起作用

Getting snapshot for some cells isn't working

我想在长按后获取单元格的快照,我让它工作了。我正在通过此代码创建快照:

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
}

它工作正常,但有时我会在输出中收到此消息:

Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.

我只对一些细胞(只有少数)得到它,但并非总是如此。有时它会从该单元格生成快照,而其他时候它 returns nil。我已经检查过,并且我总是输入单元格。那是为什么呢?为什么渲染会导致空快照?谢谢

编辑: 我在我的 tableView 中添加了手势识别器:

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)
    ...

我的 swift 解决方案 感谢 :

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
}

尝试使用来自 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];