我正在尝试从笔尖加载视图,对其进行配置并对其进行快照,而不将其添加到屏幕上:
+ (void)composeFromNibWithImage:(UIImage*)catImage completion:(CompletionBlock)completion {
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"CatNib" owner:nil options:nil];
CatView *catView = [nibContents firstObject];
//here, catView is the correct size from the nib, but is blank if inspected with the debugger
catView.catImageView.image = catImage;
catView.furButton.selected = YES;
UIImage *composite = [UIImage snapshot:catView];
completion(composite);
}
其中快照是典型的:
+ (UIImage *)snapshot:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
但是,当我逐步执行代码时,catView 和复合材料的大小都正确但空白。如何从从笔尖加载的视图创建 UIImage,而不将视图添加到屏幕?