Ios 将UIVIew转换为UIImage,而不显示UIVIew

Ios 将UIVIew转换为UIImage,而不显示UIVIew,ios,objective-c,uiview,xamarin.ios,uiimage,Ios,Objective C,Uiview,Xamarin.ios,Uiimage,我正在使用此方法将UIView转换为UIImage,当UIView(转换为UIImage)已经存在/显示时,它的工作非常出色。但我的要求是在不显示UIView的情况下将UIView转换为UIImage。不幸的是,这段代码在这种情况下失败了,我被卡住了。任何帮助都将不胜感激 我使用以下方法: + (UIImage *) imageWithView:(UIView *)view { UIGraphicsBeginImageContextWithOptions(view.bounds.size

我正在使用此方法将UIView转换为UIImage,当UIView(转换为UIImage)已经存在/显示时,它的工作非常出色。但我的要求是在不显示UIView的情况下将UIView转换为UIImage。不幸的是,这段代码在这种情况下失败了,我被卡住了。任何帮助都将不胜感激

我使用以下方法:

+ (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;
}

您的代码可能会失败,因为您没有布置视图的子视图(将视图添加为子视图时会自动完成)。试试下面我写的方法:

+ (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;

上述代码应允许您将任何
ui视图
转换为
ui图像
。理想情况下,您应该在视图中运行它(此时视图控制器将具有正确的布局大小)。如果您在实现此功能时遇到任何问题,您可以查看我为本主题的指南制作的这些示例项目!,。

假设您已经获得了工作视图,此代码应该可以将
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;

上述代码应允许您将任何
ui视图
转换为
ui图像
。理想情况下,您应该在视图中运行它(此时视图控制器将具有正确的布局大小)。如果您在实现此功能时遇到任何问题,可以查看我为本主题编写的指南示例项目!。

隐藏所有子视图,然后将UIview快照到UIImage应该可以工作,请参阅下面的代码


+(UIImage*)自定义快照屏幕视图:(UIView*)内容视图
{
如果(!contentView){
返回零;
}
CGSize size=contentView.bounds.size;
UIGraphicsBeginImageContextWithOptions(大小,否,[UIScreen Main Screen].scale);
CGRect=contentView.bounds;
[contentView drawViewHierarchyInRect:rect AfterScreen更新:是];
UIImage*image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
UIImageWriteToSavedPhotosAlbum(图像,无,无,无);
返回图像;
}
+(UIImage*)不带子视图的自定义快照屏幕:(UIView*)内容视图
{
//保存隐藏视图的哈希
NSMutableArray*hideViewsHashs=[[NSMutableArray alloc]initWithCapacity:contentView.subviews.count];
用于(contentView.subview中的UIView*子视图){
if(subview.hidden==否){
[hideViewsHashs addObject:@(subview.hash)];
NSLog(@“Dikey:video:snap:hash=%@”,@(subview.hash));
}
subview.hidden=是;
}
//查看图像
UIImage*image=[UIImage自定义_快照屏幕查看:contentView];
//恢复
用于(contentView.subview中的UIView*子视图){
if([hideViewsHashs containsObject:@(subview.hash)]){
subview.hidden=否;
NSLog(@“Dikey:video:snap:restore:hash=%@”,@(subview.hash));
}
}
//完成
返回图像;
}

隐藏所有子视图,然后将UIview快照到UIImage应该可以工作,请参见下面的代码


+(UIImage*)自定义快照屏幕视图:(UIView*)内容视图
{
如果(!contentView){
返回零;
}
CGSize size=contentView.bounds.size;
UIGraphicsBeginImageContextWithOptions(大小,否,[UIScreen Main Screen].scale);
CGRect=contentView.bounds;
[contentView drawViewHierarchyInRect:rect AfterScreen更新:是];
UIImage*image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
UIImageWriteToSavedPhotosAlbum(图像,无,无,无);
返回图像;
}
+(UIImage*)不带子视图的自定义快照屏幕:(UIView*)内容视图
{
//保存隐藏视图的哈希
NSMutableArray*hideViewsHashs=[[NSMutableArray alloc]initWithCapacity:contentView.subviews.count];
用于(contentView.subview中的UIView*子视图){
if(subview.hidden==否){
[hideViewsHashs addObject:@(subview.hash)];
NSLog(@“Dikey:video:snap:hash=%@”,@(subview.hash));
}
subview.hidden=是;
}
//查看图像
UIImage*image=[UIImage自定义_快照屏幕查看:contentView];
//恢复
用于(contentView.subview中的UIView*子视图){
if([hideViewsHashs containsObject:@(subview.hash)]){
subview.hidden=否;
NSLog(@“Dikey:video:snap:restore:hash=%@”,@(subview.hash));
}
}
//完成
返回图像;
}

只需隐藏UIView并获得图像转换。隐藏视图对我不起作用,我也尝试过。只需隐藏UIView并获得图像转换。隐藏视图对我不起作用,我也尝试过。我说它“不起作用”没有帮助。发生了什么?视图没有使用上述代码转换为图像。这仅在视图显示在屏幕上时有效。说它“不工作”没有帮助。发生了什么?视图没有使用上述代码转换为图像。这仅在视图显示在屏幕上时有效。