合并两个UIImage swift 4

时间:2019-07-16 19:59:31

标签: ios swift bitmap uiimage

我有一个从设备相机捕获的图像,我想在其上添加一个UITextView,然后将其保存到图库中。我正在使用以下代码从文本视图中获取图像

extension UITextView {
    var imageOfView: UIImage {
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, 0.0)
        self.drawHierarchy(in: self.bounds, afterScreenUpdates: true)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return image!
    }
}

然后,通过使用以下代码,我尝试将以上图像添加到拍摄的图像中:

extension UIImage {

    func combine(with image: UIImage, at point: CGPoint, isLandscapeMode: Bool = false) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.size, false, 0.0)

        self.draw(at: .zero)
        image.draw(at: point)

        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return result!
    }
}

最后,我在这里调用将已编辑的图像发送到上一页的方法:

onImageEdited?(capturedImage.combine(with: textArea.imageOfView, at: textArea.frame.origin, isLandscapeMode: true))

从下面的屏幕截图中可以看出,大小发生了变化,位置似乎是随机选择的。知道为什么以及如何解决这个问题吗?

在编辑过程中:

before saving changes

保存后:

after saving changes

PS:捕获的图像和编辑页面处于 Landscape 模式

3 个答案:

答案 0 :(得分:0)

UIImageView

您还可以为UITextView创建扩展名,并从其所有子视图(例如extension UIImageView { func renderSubviewsToImage() -> UIImage? { guard let image = image else { return nil } let scale = UIScreen.main.scale UIGraphicsBeginImageContextWithOptions(image.size, false, scale) guard let context = UIGraphicsGetCurrentContext() else { return nil } self.layer.render(in: context) let renderedImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return renderedImage } } )渲染新图像

{{1}}

答案 1 :(得分:0)

在Swift 4.2中尝试此代码

func stitchImages(images: [UIImage], isVertical: Bool) -> UIImage {
    var stitchedImages : UIImage!
    if images.count > 0 {
        var maxWidth = CGFloat(0), maxHeight = CGFloat(0)
        for image in images {
            if image.size.width > maxWidth {
                maxWidth = image.size.width
            }
            if image.size.height > maxHeight {
                maxHeight = image.size.height
            }
        }
        var totalSize : CGSize
        let maxSize = CGSize(width: maxWidth, height: maxHeight)
        if isVertical {
            totalSize = CGSize(width: maxSize.width, height: maxSize.height * (CGFloat)(images.count))
        } else {
            totalSize = CGSize(width: maxSize.width  * (CGFloat)(images.count), height:  maxSize.height)
        }
        UIGraphicsBeginImageContext(totalSize)
        for image in images {
            let offset = (CGFloat)(images.index(of: image)!)
            let rect =  AVMakeRect(aspectRatio: image.size, insideRect: isVertical ?
                CGRect(x: 0, y: maxSize.height * offset, width: maxSize.width, height: maxSize.height) :
                CGRect(x: maxSize.width * offset, y: 0, width: maxSize.width, height: maxSize.height))
            image.draw(in: rect)
        }
        stitchedImages = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }
    return stitchedImages
  }
}

,您可以使用此功能:

@IBOutlet weak var imageView: UIImageView!
let imageArray = [image1, image3, image4, image4]

override func viewDidLoad() {
    super.viewDidLoad()
    let image = stitchImages(images: imageArray, isVertical: false)
    imageView.image = image
}

答案 2 :(得分:0)

尝试一下,我已经注释了我从上述代码中编辑过的代码,请检查出来,让我知道它对您有用。

func stitchImages(images: [UIImage], isVertical: Bool) -> UIImage {
    var stitchedImages : UIImage!
    if images.count > 0 {
        var maxWidth = CGFloat(0), maxHeight = CGFloat(0)
        for image in images {
            if image.size.width > maxWidth {
                maxWidth = image.size.width
            }
            if image.size.height > maxHeight {
                maxHeight = image.size.height
            }
        }
        var totalSize : CGSize
        let maxSize = CGSize(width: maxWidth, height: maxHeight)
        if isVertical {
            totalSize = CGSize(width: maxSize.width, height: maxSize.height * (CGFloat)(images.count))
        } else {
            totalSize = CGSize(width: maxSize.width, height:  maxSize.height)
        }
        UIGraphicsBeginImageContext(totalSize)

        let offset = (CGFloat)(0)
        // Need to change or adjust the frame get get reach your reqirement
        let rect1 = AVMakeRect(aspectRatio: images[1].size, insideRect: isVertical ? CGRect(x: 0, y: maxSize.height * offset, width: maxSize.width, height: maxSize.height) : CGRect(x: maxSize.width * offset, y: 0, width: maxSize.width, height: maxSize.height))
        images[1].draw(in: rect1)

        let rect2  = AVMakeRect(aspectRatio: images[0].size, insideRect: isVertical ? CGRect(x: 0, y: maxSize.height * offset, width: maxSize.width - 60, height: maxSize.height - 500) : CGRect(x: maxSize.width * offset, y: 250, width: maxSize.width - 60, height: maxSize.height - 500))
        images[0].draw(in: rect2)

        //            for image in images {
        //                let offset = (CGFloat)(0)
        //                let rect =  AVMakeRect(aspectRatio: image.size, insideRect: isVertical ?
        //                    CGRect(x: 0, y: maxSize.height * offset, width: maxSize.width, height: maxSize.height) :
        //                    CGRect(x: maxSize.width * offset, y: 0, width: maxSize.width, height: maxSize.height))
        //                image.draw(in: rect)
        //            }

        stitchedImages = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }
    return stitchedImages
}