如何组合两个图像

时间:2017-09-16 15:54:39

标签: swift uiimage

我有一张图片。我有一个带有横幅图像的集合视图。现在,我需要将这两个图像组合成单个图像而不影响它们的质量和高度,以便我能够下载合并的图像。我搜索但找不到适合swift 3的解决方案。我的代码是:

1 个答案:

答案 0 :(得分:1)

根据您的问题您必须添加两个图像并显示在单个UIImageView中。

这是一个垂直添加两个图像并显示在UIImageView中的简单示例 -

    let topImage = UIImage(named: "image1.png") // 355 X 200
    let bottomImage = UIImage(named: "image2.png")  // 355 X 60

    let size = CGSize(width: (topImage?.size.width)!, height: (topImage?.size.height)! + (bottomImage?.size.height)!)
    UIGraphicsBeginImageContextWithOptions(size, false, 0.0)

    topImage?.draw(in: CGRect(x:0, y:0, width:size.width, height: (topImage?.size.height)!))
    bottomImage?.draw(in: CGRect(x:0, y:(topImage?.size.height)!, width: size.width,  height: (bottomImage?.size.height)!))

    let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

    // I've added an UIImageView, You can change as per your requirement.
    let mergeImageView = UIImageView(frame: CGRect(x:0, y: 200, width: 355, height: 260))

    // Here is your final combined images into a single image view.
    mergeImageView.image = newImage

我希望它能帮助你开始。