Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphonesdk将三个图像合并为两个单个图像_Iphone_Ios_Image_Ios4_Uiimageview - Fatal编程技术网

Iphonesdk将三个图像合并为两个单个图像

Iphonesdk将三个图像合并为两个单个图像,iphone,ios,image,ios4,uiimageview,Iphone,Ios,Image,Ios4,Uiimageview,我正在开发一个iphone应用程序,在这个应用程序中,我必须将三张图片合并成一张图片。我的意思是说,我有一个背景图像,一个标题图像和一个较低的图像,我需要将所有这些结合起来,形成一个单独的图像,这样我就可以用它发布到facebook上。谢谢 *编辑* 我知道这段代码适用于两个图像,但如何将其用于三个图像: UIGraphicsBeginImageContext(saveView.bounds.size); [saveView.layer renderInContext:UIGraphicsGet

我正在开发一个iphone应用程序,在这个应用程序中,我必须将三张图片合并成一张图片。我的意思是说,我有一个背景图像,一个标题图像和一个较低的图像,我需要将所有这些结合起来,形成一个单独的图像,这样我就可以用它发布到facebook上。谢谢

*编辑* 我知道这段代码适用于两个图像,但如何将其用于三个图像:

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

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

UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
然后将其转换为类似PNG的格式,例如:

NSData *imageData = UIImagePNGRepresentation(image);
那么发送应该不会太难

编辑 这是一个扩展示例,您还可以看到3幅图像-您当然可以使用Interface Builder和Outlet,而不是全部编写-但您可以复制粘贴此图像以尝试:

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,因为这样它将被渲染为不可见)。因此,要么把它从屏幕上拖下来,要么在画完后立即隐藏它。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:)];

注意:我突然意识到我的答案包括给图片添加一个标签,这就是我在找到这个之前所做的。参考资料

查看此内容-它基于opengl。我会优先考虑任何其他选择。我为您发布的代码将处理尽可能多的图像,因为手机可以在内存中处理-查看我的更新答案和扩展代码我发现我不必将referenceView添加到主视图来呈现它。然而,我确实在用我的初始代码呈现上下文时遇到了问题,但是在更改它之后,我下面的答案似乎可以正常工作。