Objective c 拍摄非';没有渲染?
假设我有一个未渲染的视图,但我想。似乎我不能使用,因为上面说: 如果当前视图尚未呈现,可能是因为它尚未出现在屏幕上,则快照视图没有可见内容 如何将UIView(例如UILabel)转换为UIImage,而无需将其添加到视图层次结构中 以下是我正在使用的代码:Objective c 拍摄非';没有渲染?,objective-c,ios7,uiview,uiimage,screenshot,Objective C,Ios7,Uiview,Uiimage,Screenshot,假设我有一个未渲染的视图,但我想。似乎我不能使用,因为上面说: 如果当前视图尚未呈现,可能是因为它尚未出现在屏幕上,则快照视图没有可见内容 如何将UIView(例如UILabel)转换为UIImage,而无需将其添加到视图层次结构中 以下是我正在使用的代码: UILabel *label = [[UILabel alloc] init]; label.text = @"Test"; [label sizeToFit]; UIGraphicsBeginImageContextWithOption
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();
这会导致快照图像显示为黑色,正如人们所期望的那样。是否有类似于SnapshotViewAfterScreen更新:的API,允许我创建UIImage而不需要渲染视图?据我所知,不需要在屏幕上渲染视图:
[label.layer renderInContext:UIGraphicsGetCurrentContext()];