【发布时间】: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