使用内部图像的实际大小在 iOS 4 中保存 UIView 内容(即放大内容以进行保存) [英] Saving UIView contents in iOS 4 with real size of the images inside (i.e. scale contentes up for save)

查看:15
本文介绍了使用内部图像的实际大小在 iOS 4 中保存 UIView 内容(即放大内容以进行保存)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIView,其中包含许多 UIImageView 作为子视图.该应用程序在 iOS4 上运行,我使用具有视网膜显示分辨率的图像(即图像加载比例 = 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.

这是,具有类似的 UIColor、UIImage、NSArray、NSDictionary 扩展...

Here is library with similar extensions for UIColor, UIImage, NSArray, NSDictionary, ...

这篇关于使用内部图像的实际大小在 iOS 4 中保存 UIView 内容(即放大内容以进行保存)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆