ios - 将两个 UIImage 并排保存为一个组合图像?

标签 ios objective-c graphics

Xcode 5、iOS 7

两个 UIView 并排 - viewA 和 viewB。 每个 View 都包含一个 UIImage - imageA 和 imageB。 两幅图像在 View 之间的边界处相遇,因此它们看起来是无缝的: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);

希望对你有帮助

关于ios - 将两个 UIImage 并排保存为一个组合图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23995567/

相关文章:

iphone - Cocos2D 与 iOS6 旋转错误

ios - 如何在另一个 UIViewController 中编辑 UICollectionView 项?

javascript - Node.js 在屏幕上绘图

java - 瓷砖网格世界创建麻烦

ios - 如何使用 Yahoo Weather API 和我的 iPad 的纬度和经度获取当前天气状况?

ios - 在函数 UITableView heightForRowAt (Swift) 中使用为 UICollectionView 构建的函数

objective-c - 尚未创建 Firebase Analytics。 ld : warning: Some object files have incompatible Objective-C category definitions

ios - 将结构与 NSMutableArray 一起使用

ios - presentingViewController 显示为 null

.net - 在平面上画线的算法