我有一个带有许多 UIImageViews 作为子视图的 UIView.该应用程序在 iOS4 上运行,我使用具有视网膜显示分辨率的图像(即图像以 scale = 2 加载)
I have an UIView with many UIImageViews as subviews. The app runs on iOS4 and I use images with retina display resolution (i.e. the images load with scale = 2)
我想保存 UIView 的内容......但是......里面有图像的真实大小.IE.视图的大小为 200x200,内部的图像比例为 2,我想保存 400x400 的结果图像和所有图像的真实大小.
I want to save the contents of the UIView ... BUT ... have the real size of the images inside. I.e. the view has size 200x200 and images with scale=2 inside, I'd like to save a resulting image of 400x400 and all the images with their real size.
现在首先想到的是创建一个新的图像上下文并再次加载其中 scale=1 的所有图像,这应该可以,但我想知道是否有更优雅的方法可以做到这一点?似乎需要大量的内存和处理器时间来重新加载所有内容,因为它已经完成了......
Now what comes first to mind is to create a new image context and load again all images inside with scale=1 and that should do, but I was wondering if there is any more elegant way to do that? Seems like a waist of memory and processor time to reload everything again since it's already done ...
附言如果有人有答案 - 包括代码会很好
p.s. if anyone has an answer - including code would be nice
实现将 任何 UIView 渲染为图像(也适用于视网膜显示).
Implementation for rendering any UIView to image (working also for retina display).
helper.h 文件:
helper.h file:
@interface UIView (Ext)
- (UIImage*) renderToImage;
@end
helper.m 文件中的归属实现:
and belonging implementation in helper.m file:
#import <QuartzCore/QuartzCore.h>
@implementation UIView (Ext)
- (UIImage*) renderToImage
{
// IMPORTANT: using weak link on UIKit
if(UIGraphicsBeginImageContextWithOptions != NULL)
{
UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0.0);
} else {
UIGraphicsBeginImageContext(self.frame.size);
}
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
0.0 是比例因子.应用于位图的比例因子.如果您指定值 0.0,则比例因子设置为设备主屏幕的比例因子.
0.0 is the scale factor. The scale factor to apply to the bitmap. If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen.
QuartzCore.framework也应该放入项目中,因为我们在图层对象上调用函数.
QuartzCore.framework also should be put into the project because we are calling function on the layer object.
要在 UIKit 框架上启用弱链接,请单击左侧导航器中的项目项,单击项目目标 -> 构建阶段 -> 链接二进制并在 UIKit 框架上选择可选"(弱)类型.
To enable weak link on UIKit framework, click on the project item in left navigator, click the project target -> build phases -> link binary and choose "optional" (weak) type on UIKit framework.
这里是 library,具有类似的 UIColor、UIImage、NSArray、NSDictionary 扩展...
Here is library with similar extensions for UIColor, UIImage, NSArray, NSDictionary, ...
这篇关于在 iOS 4 中将 UIView 内容保存为内部图像的实际大小(即放大内容以进行保存)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!