【问题标题】:Stitch multiple images together horizontally (Swift 3)将多个图像水平拼接在一起(Swift 3)
【发布时间】:2019-01-23 19:09:07
【问题描述】:

我想将多张图像水平拼接在一起并将其保存为一张图像。在this 问题下,我找到了这个建议的解决方案:

import UIKit
import AVFoundation

    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
}

但是,我不知道如何使用它。有人可以举例说明如何将其与图像数组一起使用吗?

谢谢!

【问题讨论】:

  • 有什么问题?只需通过传递图像数组来使用该方法并获取单个图像。
  • 是的,我想通了!

标签: ios swift


【解决方案1】:

有一点更新 斯威夫特 4

extension Array where Element: UIImage {
    func stitchImages(isVertical: Bool) -> UIImage {

        let maxWidth = self.compactMap { $0.size.width }.max()
        let maxHeight = self.compactMap { $0.size.height }.max()

        let maxSize = CGSize(width: maxWidth ?? 0, height: maxHeight ?? 0)
        let totalSize = isVertical ?
            CGSize(width: maxSize.width, height: maxSize.height * (CGFloat)(self.count))
            : CGSize(width: maxSize.width  * (CGFloat)(self.count), height:  maxSize.height)
        let renderer = UIGraphicsImageRenderer(size: totalSize)

        return renderer.image { (context) in
            for (index, image) in self.enumerated() {
                let rect = AVMakeRect(aspectRatio: image.size, insideRect: isVertical ?
                    CGRect(x: 0, y: maxSize.height * CGFloat(index), width: maxSize.width, height: maxSize.height) :
                    CGRect(x: maxSize.width * CGFloat(index), y: 0, width: maxSize.width, height: maxSize.height))
                image.draw(in: rect)
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    真的吗?

    您找到的代码正是您要查找的代码,但您不知道如何调用它?

    如果您不知道如何调用函数,则很难以一种有用的方式帮助您。下面是一些通用代码,可以加载图像数组并将它们拼接在一起:

    //Create an array of image names
    let imageNames = ["Fish", "Dog", "Eggplant", "Wombat"]
    
    //create an array of images with those names (images must exist in app bundle)
    let images = imageNames.flatMap{UIImage(named:$0)}
    
    //Stitch the images together horizontally
    let stichedImage = stitchImages(images: images, isVertical: false)
    

    您可能应该停下来阅读一下 Swift。我建议下载 Apple Swift 书并阅读它。它非常好,而且很容易遵循。这就是我学习语言的方式。

    【讨论】:

    • 您遇到了什么问题?
    • 我在使用阵列中的图像时遇到了问题,但我解决了!
    • 如果我的回答解决了您的问题,您应该接受我的回答,或者如果您认为我的回答没有解决您的问题,请发布您自己的解决方案。说“我想通了”并不能帮助其他人阅读这个问题,这是本网站的重点。
    猜你喜欢
    • 1970-01-01
    • 2015-05-03
    • 2014-08-11
    • 1970-01-01
    • 2011-12-26
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    相关资源
    最近更新 更多