iPhone SDK合并三张图片成两张单独的图片。

6
我正在开发一个iPhone应用程序,需要合并三张图片并将它们变成一张单独的图片。我的意思是说,我有一个背景图片、一个标题图片和一个底部图片,我需要将它们组合在一起,以便我可以将其发布到Facebook上。谢谢。
*编辑* 我知道如何将两张图片合并,但如何使用它来合并三张图片呢?
UIGraphicsBeginImageContext(saveView.bounds.size);
[saveView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

1
请查看此链接 - https://dev59.com/3FXTa4cB1Zd3GeqPzTI1 - Srikar Appalaraju
它基于OpenGL。我更倾向于使用其他替代方案。 - Syed Faraz Haider Zaidi
我为您发布的代码将在内存中手机可以处理的所有图像上运行 - 请查看我的更新答案,其中包含扩展代码。 - shein
2个回答

10

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

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

然后将其转换为特定格式,例如PNG:

NSData *imageData = UIImagePNGRepresentation(image);

那么发送应该不会太困难。

编辑 这里有一个扩展示例,你还可以看到3张图片 - 当然你也可以使用界面构建器和插座而不是编写所有内容 - 但你可以复制粘贴此内容以尝试:

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,否则它将被渲染为不可见。因此,在绘图后要么将其放置在屏幕外,要么立即隐藏。


我发现我不必将referenceView添加到主视图中进行渲染。然而,我在使用初始代码渲染上下文时遇到了问题,但在更改后,我的答案似乎可以正常工作。 - Christopher

2

Shein的回答帮助了我,但我发现我不必将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:)];

注意:我突然意识到我的答案包括将标签添加到图像中,这正是我在找到这个之前想要做的。参考将标签文本与图像组合


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接