Iphonesdk 合并三张图片 两张单张图片

Iphonesdk Merge three images two single image(Iphonesdk 合并三张图片 两张单张图片)

本文介绍了Iphonesdk 合并三张图片 两张单张图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 iphone 应用程序,我必须合并三个图像并使其成为单个图像.我的意思是说我有一个背景图像、一个标题图像和一个下部图像,我需要将所有这些组合成一个图像,这样我就可以用它来发布到 Facebook.谢谢.

i am working on iphone app where i have to merge three images and make them single image. I mean to say i have a background image, a header image and lower image, i need to combine all this to make a single image so i can use it to post to the facebook. thanks.

*编辑*我知道这两个图像的代码,但我怎么能用它来处理三个图像:

*EDIT* i know this code for two images but how can i use it for three images :

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

推荐答案

您可以在一个 UIView 中将它们对齐(我认为甚至在屏幕外,但我还没有检查) - 然后将该 UIView 转换为使用 QuartzCode 的 UIImage:

You can just align them together in a single UIView ( I think even off-screen but I haven't checked yet) - and then just convert that UIView to a UIImage using QuartzCode:

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

然后将其转换为格式 - 例如 PNG:

Then turn that into a format - like PNG for instance:

NSData *imageData = UIImagePNGRepresentation(image);

那么发送应该不会太难.

Then sending shouldn't be too difficult.

编辑这是一个扩展示例,您也可以看到 3 张图片 - 您当然可以使用 Interface Builder 和 Outlets,而不是全部编写 - 但您可以复制粘贴来尝试:

EDIT Here is an extended example you can also see for 3 images - you can of course use Interface Builder and Outlets instead of writing it all - but you can copy paste this to try:

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,因为那样它将被渲染为不可见).所以要么把它放在屏幕外,要么在绘制后立即隐藏它

NOTE I've checked and the UIView must be drawable/visible at the time you call renderInContext (it can be off-screen but it cannot be hidden or alpha=0 because then it will be rendered invisible). So either put it off-screen or immediately hide it after drawing

这篇关于Iphonesdk 合并三张图片 两张单张图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Iphonesdk 合并三张图片 两张单张图片

基础教程推荐