我正在使用方法将UIView转换为UIImage,当UIView(要转换为UIImage)已经存在/显示时,它做得很好。但我的要求是将UIView转换为UIImage而不显示UIView。不幸的是,这个代码在这种情况下失败了,我被困住了。任何帮助将不胜感激。
我使用以下方法:
+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [[UIScreen mainScreen] scale]);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
答案 0 :(得分:1)
您的代码可能会失败,因为您没有布置视图的子视图(当您将视图添加为子视图时会自动完成)。尝试类似我在下面写的方法:
+ (UIImage *)imageFromView:(UIView *)view sized:(CGSize)size
{
// layout the view
view.frame = CGRectMake(0, 0, size.width, size.height);
[view setNeedsLayout];
[view layoutIfNeeded];
// render the image
UIGraphicsBeginImageContextWithOptions(size, view.opaque, 0.0f);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return renderedImage;
}
答案 1 :(得分:0)
假设您已经有了工作视图,此代码应该可以将UIView
转换为UIImage
(我用它来将渐变转换为图像并显示图像到UIProgressView
。
夫特:
let renderer = UIGraphicsImageRenderer(size: gradientView.bounds.size)
let image = renderer.image { ctx in
gradientView.drawHierarchy(in: gradientView.bounds, afterScreenUpdates: true)
}
目标C:
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:gradientView.bounds.size];
UIImage *gradientImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
[gradientView drawViewHierarchyInRect:gradientView.bounds afterScreenUpdates:true];
}];
_progressView.progressImage = gradientImage;
上述代码应该允许您将任何UIView
转换为UIImage
。理想情况下,您应该在viewWillAppear中运行它(此时视图控制器将具有正确的布局大小)。如果你有任何问题,你可以看看我为这个主题的指南做的这些示例项目! Objective C,Swift。
答案 2 :(得分:0)
隐藏所有子视图,然后将UIview快照复制到UIImage应该起作用,请参见下面的代码
+ (UIImage *)custom_snapshotScreenInView:(UIView *)contentView
{
if (!contentView) {
return nil;
}
CGSize size = contentView.bounds.size;
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
CGRect rect = contentView.bounds;
[contentView drawViewHierarchyInRect:rect afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
return image;
}
+ (UIImage *)custom_snapshotScreenWithoutSubviews:(UIView *)contentView
{
// save hidden view's hash
NSMutableArray *hideViewsHashs = [[NSMutableArray alloc]initWithCapacity:contentView.subviews.count];
for (UIView *subview in contentView.subviews) {
if (subview.hidden == NO) {
[hideViewsHashs addObject:@(subview.hash)];
NSLog(@"Dikey:video:snap:hash = %@", @(subview.hash));
}
subview.hidden = YES;
}
// view to image
UIImage *image = [UIImage custom_snapshotScreenInView:contentView];
// restore
for (UIView *subview in contentView.subviews) {
if ([hideViewsHashs containsObject:@(subview.hash)]) {
subview.hidden = NO;
NSLog(@"Dikey:video:snap:restore:hash = %@", @(subview.hash));
}
}
// finish
return image;
}