Iphonesdk合并三张图片两张单张图片 [英] Iphonesdk Merge three images two single image

查看:112
本文介绍了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.

* 编辑 *
i知道两个图片的代码,但我该如何使用它三张图片:

*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中将它们对齐在一起(我认为即使在屏幕外,我还没有检查过) - 然后使用QuartzCode将UIView转换为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; 

注意
我已经检查过并且UIView必须是在调用renderInContext时可绘制/可见(它可以在屏幕外但不能隐藏或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合并三张图片两张单张图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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