Javascript 获取类中的层内容
我有一个自定义的UIImageView类,它创建缩略图(UIImageView),每个缩略图都有一个标签 标签由另一个类创建。label类在其上创建UIImageView和UITextView 这是主缩略图类的init方法:Javascript 获取类中的层内容,javascript,iphone,iphone-sdk-3.0,Javascript,Iphone,Iphone Sdk 3.0,我有一个自定义的UIImageView类,它创建缩略图(UIImageView),每个缩略图都有一个标签 标签由另一个类创建。label类在其上创建UIImageView和UITextView 这是主缩略图类的init方法: - (id)initWithFrame:(CGRect)frame { if ([super initWithFrame:frame] == nil) { return nil; } CGRect myFrame = CGRectMake
- (id)initWithFrame:(CGRect)frame
{
if ([super initWithFrame:frame] == nil) {
return nil;
}
CGRect myFrame = CGRectMake(0, 0, 100, 100);
myLabel = [[myLabelClass alloc] initWithFrame: myFrame]; //myLabelClass is a UIImageView based class
[myLabel setCenter:CGPointMake(50, 50)];
[self addSubview: myLabel];
return self;
}
label类只创建一个UIImageView和一个UITextView,并在其上放置文本
所以,我有这个
MAIN VIEW CONTROLLER |
|___ UIImageView WITH LABEL
|
|____ label background (UIView)
|____ UITEXTVIEW (text)
现在我想把这三个组件的内容都写到quartz上下文中
我需要使用drawInRect进行写入,因为我需要将完整对象写入精确的位置
我希望object.layer.contents是与这3个“层”展平的图像等效的图像,换句话说,对象的图像、标签背景和标签文本,就像我在Photoshop中创建这3个对象并展平构图一样
我还希望缩略图.myLabel.layer.contents能够
包含标签背景上UITextView的呈现内容
问题是当我使用
UIImage *myImage = [UIImage imageWithCGImage:thumbnail.myLabel.layer.contents];
[myImage drawInRect:CGRectMake(0, 0, 100, 100)];
我什么也得不到
如何获得完整对象(包括其子视图)的“展平”图像
谢谢仅当您设置CALayer内容时,它才有效。尝试使用
UIGraphicsBeginImageContext
创建上下文,并使用CALayerrenderInContext
创建图像。我将这样的内容用作UIView的一个类别:
-(UIImage *) asImage {
UIImage *result = nil;
UIGraphicsBeginImageContext( [self bounds].size );
[[self layer] renderInContext:UIGraphicsGetCurrentContext()];
result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
伙计,你真是个天才!谢谢最简单的方法显然是在类内部进行渲染,但我没有看到它!:-)