【发布时间】:2018-06-27 12:01:51
【问题描述】:
我进行了很多搜索,并尝试了在 StackOverflow 中可以找到的每一种方法,但都没有奏效。假设我有一个 UIView,它的 UIImageView 包含一个尺寸为 1800x2300 的 UIImage,这显然大于 iPhone 7/8 屏幕的尺寸,因此即使使用屏幕比例也无助于渲染包含此图像的此视图。是的,我也尝试过https://stackoverflow.com/questions/4334233/how-to-capture-uiview-to-uiimage-without-loss-of-quality-on-retina-display,我想要一个比我的 Retina 屏幕尺寸更大的渲染,但它不适合我。这些是我尝试这样做的方法,它们不会渲染任何大于我的 imageRect * Scale of my screen 的大小
// 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 * ScreenScale 的大小,但我想从我的 UIView (_viewWithLoadedImages) 获得更大的输出图像,我该怎么办?我几乎尝试了一切。 非常感谢任何帮助。
【问题讨论】:
标签: objective-c uiview uiimageview uiimage uigraphicscontext