[英]Covert UIVIew to UIImage without presenting/displaying UIView
我正在使用方法将 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;
}
您的代码可能会失败,因为您没有布置视图的子视图(当您将视图添加为子视图时会自动完成)。 试试我下面写的方法:
+ (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;
}
假设您已经有了一个工作视图,这段代码应该可以将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 中运行它(因为此时视图控制器将具有正确的布局大小)。 如果您在使用它时遇到任何问题,您可以查看我为该主题制作的指南的这些示例项目! 目标 C ,斯威夫特。
隐藏所有子视图,然后将 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.