【问题标题】:Iphonesdk Merge three images two single imageIphonesdk 合并三张图片 两张单张图片
【发布时间】:2011-10-31 12:46:44
【问题描述】:

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

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

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

【问题讨论】:

  • 它基于opengl。我会首选任何其他选择。
  • 我为您发布的代码将适用于手机在内存中可以处理的尽可能多的图像 - 请参阅我的更新答案和扩展代码

标签: iphone ios image ios4 uiimageview


【解决方案1】:

您可以在一个 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; 

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

【讨论】:

  • 我发现我不必将 referenceView 添加到主视图来呈现它。但是,我在使用初始代码渲染上下文时确实遇到了问题,但是在更改它之后,我的答案似乎可以正常工作。
【解决方案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

【讨论】:

    猜你喜欢
    • 2011-11-04
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多