我正在使用此代码将UIView呈现为图像 -(UIImage *)getImageForView:(UIView *)aView { UIGraphicsBeginImageContextWithOptions(aView.bounds.size, NO, 0.0);[aView.layer renderInContext:UIGraphicsGetCurrentContext()];UIImage *viewImage
-(UIImage *)getImageForView:(UIView *)aView { UIGraphicsBeginImageContextWithOptions(aView.bounds.size, NO, 0.0); [aView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return viewImage; }
我遇到的问题是使用autolayout未正确设置视图.我该怎么办.
我目前创建一个UIView“viewA”,其框架是我视图的正确大小,然后创建我复杂的预期视图“ViewB”(其上有自动布局),然后将viewB作为子视图添加到“viewA”然后我给viewA到我上面的方法.
首先我认为viewA不必添加到某个superview但它没有正确布局,如果我将viewA与alpha为0添加到superview,那么我假设可能会使用“layoutIfNeeded”但是应该在哪个视图上我这样做?
编辑:这是创建UIView的代码
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 439)]; //This is the UIView that contains a lot of views MyView *myView = [[MyView alloc] init]; myView.translatesAutoresizingMaskIntoConstraints = NO; [view addSubview:myView]; [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[myView(439)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(myView)]]; [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[myView(320)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(myView)]];
最终为我工作的是什么
[myView layoutIfNeeded]; [view layoutIfNeeded];
换句话说,在两个视图上调用它.
在图像上下文中呈现layoutIfNeeded消息之前,先发送视图.