将两个UIImages并排保存为一个合并图像



Xcode 5, iOS 7

两个并排的uiview - viewA和viewB。每个视图包含一个UIImage - imageA和imageB。两个图像在视图之间的边界相遇,使它们看起来无缝:imageAimageB.

如何将两个图像保存到一个图像文件中,并排,就像它们是一个图像一样?

我知道我可以截屏,但这会降低分辨率,并且不能解释可能在屏幕外的部分图像(由于缩放或定位)。

这可能会回答我自己的问题,但我能想到的最好的是创建一个新的UIImage (imageC),大小为imageA和imageB,然后根据图像的相对位置将图像复制到imageC。

有更简单的方法吗?

在你的界面中使用2个UIImageView,在为每个UIImagePicker使用之后,你可以用下面的代码进行管理:

    - (IBAction)margeSave:(id)sender{
        //here you get you two different image
        UIImage *bottomImage = self.imageViewPick.image;
        UIImage *image       = self.myImage.image;
        //here  you have to crop each image with the code below
        //using here a crop code and adjust for your Image
        // create a new size for a merged image
        CGSize newSize = CGSizeMake(640, 640);
        UIGraphicsBeginImageContext( newSize );
        [bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
        [myImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:1.0];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);
        //option if you are in other view when save
        //[self.navigationController popViewControllerAnimated:YES];

    }

您可以集成以下代码来裁剪图像:

在InterfaceBuilder中使用2部分图像来选择您想要的特定大小,例如在iPhone上使用2 UIImageView W:150 H:300 total il a 300x300并使用大小裁剪图像。

CGRect clippedRect  = CGRectMake(self.view.frame.origin.x+91, self.view.frame.origin.y, self.view.frame.size.width-91*2, self.view.frame.size.height-220);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);
UIImage *imageCrop   = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

希望对你有帮助

如果你真的想把这些图片保存为一个文件,那么看看这里的这篇文章。你将需要一个图形上下文和所有这些东西。只是要确保适当地设置它们的高度、宽度和位置值,以免重叠。

否则,如果你只是想在内存中做这个,用两个图像的尺寸创建一个UIView,并将UIImageViews添加到这个UIView。确保正确设置它们的框架

UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
[container addSubview:image1];
[container addSubview:image2];
image1.frame = CGRectMake(0, 0, 150, 300);
image2.frame = CGRectMake(150, 0, 150, 300);

然后移动这个容器或者你想怎么做就怎么做

相关内容

最新更新