【问题标题】:Taking a snapshot of view that isn't rendered?拍摄未渲染视图的快照?
【发布时间】:2014-04-07 19:44:12
【问题描述】:

假设我有一个未呈现的视图,但我想convert it to a UIImage。好像我不能使用snapshotViewAfterScreenUpdates:,因为它说:

如果当前视图还没有渲染,可能是因为它还没有出现在屏幕上,快照视图没有可见的内容。

如何将 UIView(例如 UILabel)转换为 UIImage 而无需将其添加到视图层次结构中?

这是我正在使用的代码:

UILabel *label = [[UILabel alloc] init];
label.text = @"Test";
[label sizeToFit];

UIGraphicsBeginImageContextWithOptions(label.bounds.size, NO, 0.0f);
[label drawViewHierarchyInRect:label.bounds afterScreenUpdates:NO];
UIImage * snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

这会导致 snapshotImage 显示为黑色,正如人们所预料的那样。是否有类似于snapshotViewAfterScreenUpdates: 的 API 可以让我创建 UIImage 而无需渲染视图?

【问题讨论】:

    标签: objective-c ios7 uiview uiimage screenshot


    【解决方案1】:

    renderInContext:,据我所知,不需要在屏幕上渲染视图:

    [label.layer renderInContext:UIGraphicsGetCurrentContext()];
    

    【讨论】:

      猜你喜欢
      • 2015-12-12
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      相关资源
      最近更新 更多