Objective c 如何在不使用屏幕比例的情况下,将UIView/UIImageView渲染为具有图像精确大小的UIImage?

Objective c 如何在不使用屏幕比例的情况下,将UIView/UIImageView渲染为具有图像精确大小的UIImage?,objective-c,uiview,uiimageview,uiimage,uigraphicscontext,Objective C,Uiview,Uiimageview,Uiimage,Uigraphicscontext,我在StackOverflow中搜索了很多,尝试了每一种方法,但都没有成功。假设我有一个UIView,它有一个UIImageView,其中包含一个大小为1800x2300的UIImage,这显然比iPhone 7/8的屏幕大,所以即使使用屏幕比例也无助于渲染包含此图像的视图。是的,我也尝试过,我想要一个比我的视网膜屏幕大小大的渲染,但它对我没有帮助。这些是我尝试的方法,它们不会渲染任何比我的imageRect*屏幕大小更大的东西 // Approach 1 @autoreleas

我在StackOverflow中搜索了很多,尝试了每一种方法,但都没有成功。假设我有一个UIView,它有一个UIImageView,其中包含一个大小为1800x2300的UIImage,这显然比iPhone 7/8的屏幕大,所以即使使用屏幕比例也无助于渲染包含此图像的视图。是的,我也尝试过,我想要一个比我的视网膜屏幕大小大的渲染,但它对我没有帮助。这些是我尝试的方法,它们不会渲染任何比我的imageRect*屏幕大小更大的东西

    // Approach 1
    @autoreleasepool{
        //imageRect is a CGRect which is the rect I defined for my 
        //UIImageView to be aspect fitted in it.
        //_viewWithLoadedImages is the UIView containing two transparent 
        //UIImageViews on top of each other, each containing images 
        //bigger than screen size of iPhones.

        UIGraphicsBeginImageContextWithOptions(imageRect.size,NO,0);
        CGContextRef c = UIGraphicsGetCurrentContext();
        CGContextConcatCTM(c, CGAffineTransformMakeTranslation(-imageRect.origin.x, -imageRect.origin.y));
        [_viewWithLoadedImages.layer renderInContext:c];
        UIImage*renderedImage = 
        UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return renderedImage;
    }

    //Approach 2
    @autoreleasepool{
        UIGraphicsImageRenderer * Renderer = [[UIGraphicsImageRenderer alloc] initWithBounds:imageRect];
        self.tempImage=[Renderer imageWithActions:^(UIGraphicsImageRendererContext*_Nonnull context){[_viewWithLoadedImages.layer renderInContext:context.CGContext];}];
            return self.tempImage;
    }

    //Approach 3 
    UIGraphicsBeginImageContextWithOptions(imagerect, NO, 0.0f);
    [_viewWithLoadedImages drawViewHierarchyInRect:imagerect afterScreenUpdates:YES];
    UIImage * snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return snapshotImage;
它们的输出大小都等于ImageRect*屏幕缩放的大小,但我希望从UIView(_ViewWithLoadeImage)获得更大的输出图像,我该怎么办?我几乎什么都试过了。
非常感谢您的帮助。

假设您有两个UIImageView,并且可以使用其中一个来调整最终图像的大小,您可以这样做

// Desired dimension in pixels
CGRect frame = CGRectMake(0.0, 0.0,
                          _imageView1.image.size.width * _imageView1.image.scale,
                         _imageView1.image.size.height * _imageView1.image.scale);

// Use scale 1.0 for pixel size
UIGraphicsBeginImageContextWithOptions(frame.size, NO, 1.0);

// Draw the images on top of each other
[_imageView1.image drawInRect:frame];
[_imageView2.image drawInRect:frame];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image;

到底是什么不起作用?我自己也试过了,它会以全分辨率渲染图像。它确实有效
UIGraphicsBeginImageContextWithOptions
创建一个新的当前上下文,并且
drawInRect
将在该上下文中绘制。我在按钮按下事件中测试了它,而不是在drawRect中。我不确定我是否看到了真正的问题。如果我没有弄错的话,您可以看到各种视图,其中可能包含缩小的合成图像。然后,您想获得这样一个视图的全分辨率图像表示,以便在其他地方使用吗?如果是这样,那么这就是一种方法。如果您想要一种更通用的方法,那么可以将它放在一个以父视图为参数的方法中,并让该方法在其中的所有图像视图中循环以创建结果图像。使用
CGContextRef context=UIGraphicsGetCurrentContext()获取上下文。如果不起作用,可以在渲染子视图之前将掩码应用于每个子视图。您需要将掩码应用于您的上下文。我认为这种混合模式应该可以做到这一点。如果你在那之后画了什么画,别忘了把它改回去<代码>CGContextSetBlendMode(上下文,kCGBlendModeDestinationIn)
[\u bgImageView.layer.mask drawInContext:context]