2011-08-21 88 views
6

我对iPhone应用程序工作在那里我有合并三个图像,使它们单个图像。我的意思是说我有一个背景图像,一个标题图像和较低的图像,我需要结合所有这些才能制作单个图像,以便我可以使用它发布到Facebook。谢谢。Iphonesdk合并三个图像的两个单个图像

* 编辑 * 我知道两个图像此代码,但我怎么可以使用它的三幅影像:

UIGraphicsBeginImageContext(saveView.bounds.size); 
[saveView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
+1

检查了这一点 - http://stackoverflow.com/questions/5299452/how-can-i-crop-an-image-with-mask-and-combine-it-with-another-image-background –

+0

它是基于opengl的。我会pref是任何其他的选择。 –

+0

我贴你将工作作为手机可以在内存中处理多达图像的代码 - 见我与扩展代码 – shein

回答

10

你可以只在一个单一的UIView排列在一起(我想即使关屏,但我还没有选中) - 然后只用QuartzCode说的UIView转换为一个UIImage:

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

然后把它转换成一种格式 - 像PNG在立场:

NSData *imageData = UIImagePNGRepresentation(image); 

然后发送不应该太难。

编辑 这里是一个扩展的例子,你还可以看到3张图片 - 当然,你可以使用Interface Builder与出路,而不是写这一切的 - 但你可以复制粘贴此尝试:

UIImageView *imgView1, *imgView2, *imgView3; 
imgView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1"]]; 
imgView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image2"]]; 
imgView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image3"]]; 

imgView1.frame = CGRectMake(0, 0, 50, 50); 
imgView2.frame = CGRectMake(50, 50, 100, 100); 
imgView3.frame = CGRectMake(100, 100, 200, 200); 

[referenceView addSubview:imgView1]; 
[referenceView addSubview:imgView2]; 
[referenceView addSubview:imgView3]; 

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

resultView = [[UIImageView alloc] initWithImage:finalImage]; 
resultView.frame = CGRectMake(0, 0, 320, 480); 
[self.view addSubview:resultView]; 

referenceView.hidden = YES; 

注意 我已经检查过,当你调用renderInContext的时候UIView必须是可绘制/可见的(它可以不在屏幕上,但它不能被隐藏或者alpha = 0,因为那样它将被渲染为不可见)。因此,要么把它关闭屏幕或立即绘制

+0

更新的答案,我发现我没有给referenceView添加到主视图渲染。但是,我确实遇到了使用我的初始代码渲染上下文的问题,但在更改后,我的答案似乎可以正常工作。 – Christopher

2

幸的回答后隐藏它帮助我,但我发现我没有到的UIView添加到主视图,以呈现它。这里是添加文字到图像的例子...

UIImage *image = [UIImage imageNamed:@"toolbar_icon"]; // image is 20 x 20 .png 

    UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)]; 
    [tempView addSubview:[[UIImageView alloc] initWithImage:image]]; 

    UILabel *label = [[UILabel alloc] initWithFrame:tempView.frame]; 
    [label setText:[NSString stringWithFormat:@"%d", [self countValue]]]; 
    [label setFont:[UIFont systemFontOfSize:22.0]]; 
    [label setTextColor:[UIColor lightTextColor]]; 
    [label setTextAlignment:UITextAlignmentCenter]; 
    [label setBackgroundColor:[UIColor clearColor]]; 
    [tempView addSubview:label]; 

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

    UIBarButtonItem *countItem = [[UIBarButtonItem alloc] initWithImage:finalImage 
                     style:UIBarButtonItemStyleBordered 
                     target:self 
                     action:@selector(countAction:)]; 

注:我突然意识到我的回答包括添加标签,这正是我试图找到在此之前做的图像。参考Combine Label Text With Image