【问题标题】:Saving two UIImages side-by-side as one combined image?将两个 UIImage 并排保存为一个组合图像?
【发布时间】:2014-06-02 13:20:15
【问题描述】:

Xcode 5、iOS 7

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

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

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

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

有更简单的方法吗?

【问题讨论】:

标签: ios objective-c graphics


【解决方案1】:

在你的界面中使用 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);

希望对你有帮助

【讨论】:

  • 这确实有帮助,但它会导致一个额外的问题 - 如果我只想保存原始图像的一部分 - 也许每个图像的左半部分?谢谢。
  • 抱歉 - 无法编辑我上面的评论 - 更详细地说,我正在尝试保存每张图像的特定部分,例如左侧的 30% 和右侧的 70%。
  • 是否新的“UIImage *newImage = [UIImage imageWithCGImage:imageRef];”替换“UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();” ?
  • 否,因为在这种情况下不要合并,抱歉使用了我的旧代码,您必须创建其他实例“UIImage *imageCrop = [UIImage imageWithCGImage:imageRef];”然后用于新图像我重写代码更好;)
  • 这些不是并排的;它们是重叠的。
【解决方案2】:

如果您确实想将图像作为单个图像保存到文件中,请查看此帖子here。您将需要一个图形上下文和所有这些东西。只需确保适当地设置它们的高度、宽度和位置值,以免重叠。

否则,如果您只想在内存中执行此操作,请创建一个包含两个图像尺寸的 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);

然后只需在容器周围移动或随心所欲地使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多