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

时间:2011-08-21 09:20:38

标签: iphone ios image ios4 uiimageview

我在iphone应用程序上工作,我必须合并三个图像并使它们成为单个图像。我的意思是说我有一个背景图像,一个标题图像和较低的图像,我需要将所有这些组合成一个单一的图像,所以我可以使用它发布到Facebook。感谢。

*的 修改 * 我知道这两个图像的代码,但我如何将它用于三个图像:

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

2 个答案:

答案 0 :(得分: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和Outlets而不是全部编写 - 但是您可以复制粘贴以尝试:

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,因为它将呈现为不可见)。所以要么把它放在屏幕外,要么在绘制后立即隐藏它

答案 1 :(得分: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:)];

注意:我突然意识到我的答案包括为图像添加标签,这是我在发现之前尝试做的事情。参考Combine Label Text With Image