在Swift中合并2个图像而不会丢失原始图像的大小 [英] Merge 2 images in Swift without losing the size of the original image

查看:108
本文介绍了在Swift中合并2个图像而不会丢失原始图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试合并2张图片而不会丢失原始图像的大小。
我有一个大的白色图像,我想将一个较小的图像合并到它上面。如果我移动较小的图像,使其只有较大的图像显示在较大的图像上,那么当我合并2个图像时,只有正在显示的图像的一半应该合并,另一边应该被裁剪掉。



下图中的层次结构



UIView:检查剪辑子视图



---->带白色图像的UIImageView



----> UIImageView:此图像有一个平移手势,允许它在视图中移动。



按钮之前



我发现这篇文章



有谁知道如何合并2张图片并考虑合并时的位置?理想情况下,第二个图像看起来与第一个图像完全相同,只不过它是单个图像而不是两个图像。

解决方案

一种能给你想要的结果的合理技巧是


  1. 将您想要合并的图像排列到UIView中。

  2. 拍摄UIView的图像快照。

我喜欢在UIView上创建扩展图像(如果你愿意,可以拍摄一个屏幕截图)。因此: -

  import UIKit 

扩展名UIView {

func capture () - > UIImage {

UIGraphicsBeginImageContextWithOptions(self.frame.size,self.opaque,UIScreen.mainScreen()。scale)
self.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

返回图片
}

}

一旦您的图像(作为子视图)被单个视图包含,您就可以。

  let mergedImage = containerUIView.capture()


I'm trying to merge 2 images without losing the size of the original image. I have a large white image and I want to merge a smaller image onto it. If I move the smaller image so that only have of it is showing on top of the larger image, then when I merge the 2 images only the half of the image that is showing should be merged and the other side should be cropped out.

Hierarchy of the image below

UIView : Clip Subviews is checked

----> UIImageView with white image

----> UIImageView : This image has a pan gesture allowing it move within the view.

I found this post merge two different images swift and tried to mask the images

let maskedImage: UIImage = self.maskImage(imageView.image!, withMask: whiteImage.image!)

newImageView.image = maskedImage

but that doesn't take into account the images current positions in the image views. It just returns the original image (which is then resized to fit the UIIMageView and that's why its a little larger than before)

Does anyone know how can I merge the 2 images and take into account their positions when merging? Ideally the second image looks exactly like the first image except that's it's a single image instead of two.

解决方案

A reasonable technique which would give you a desired result would be to

  1. Arrange the images you wish to 'merge' into a UIView.
  2. Take an image snapshot of that UIView.

I like to create extensions on UIView which will snapshot an image (take a screen shot if you will). Thus:-

import UIKit

extension UIView {

  func capture() -> UIImage {

    UIGraphicsBeginImageContextWithOptions(self.frame.size, self.opaque, UIScreen.mainScreen().scale)
    self.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return image
  }

}

Once your images (as subviews) are contained by a single view you can.

let mergedImage = containerUIView.capture()

这篇关于在Swift中合并2个图像而不会丢失原始图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆