创建部分屏幕的图像?

时间:2011-03-27 22:26:00

标签: iphone objective-c cocoa-touch uiview screenshot

我有一系列子画面在我的应用程序的屏幕的一部分上创建,并希望它采取这个并将其保存为图像,然后显示图像,希望使应用程序运行更快。如何在这些图层中保存图像(暂时无法在照片应用中访问)?如果有帮助的话,他们都在UIView里面。

3 个答案:

答案 0 :(得分:1)

你可以通过截图找到你想要的东西:

UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); 

答案 1 :(得分:1)

我创建了这个类别:

<强>·H

#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>

@interface UIView (ViewCapture)
    - (UIImage*)captureView;
@end

<强>的.m

#import "UIView+ViewCapture.h"

@implementation UIView (ViewCapture)

- (UIImage*)captureView
{    
    CGRect rect = self.frame;  
    UIGraphicsBeginImageContext(rect.size);  
    CGContextRef context = UIGraphicsGetCurrentContext();  
    [self.layer renderInContext:context];  
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext();  
    return img;    
}
@end

像这样使用:

UIImage *screenshot = [aView captureView];

Save and Load UIImage in Documents directory on iPhone

答案 2 :(得分:0)

使用UIGraphicsBeginImageContext和相关功能,如下所示(创建按钮图像):

UIGraphicsBeginImageContext(button.frame.size);
CGContextRef theContext = UIGraphicsGetCurrentContext();
[button.layer renderInContext:theContext];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
NSData *theData = UIImagePNGRepresentation(theImage);
UIGraphicsEndImageContext();    

对于一个工作示例(实际上将图像保存在模拟器中),请参阅https://github.com/dermdaly/ButtonMaker