chewy chewy - 3 months ago 35
Swift Question

combine an array of images to a single image which is a row of images in swift

Trying to combine an array of images to a single image which looks like a long row of the images.
(note: images may or may not be of similar sizes)

enter image description here

here is the code, which currently works for single size images only

func combine(images: NSMutableArray) -> UIImage {
var contextSize = CGSizeZero
var xMaxSize : CGFloat = 0
for image in images {
//

xMaxSize = xMaxSize + image.size.width
//contextSize.width = max(contextSize.width , image.size.width )
contextSize.height = max(contextSize.height, image.size.height)
}
contextSize.width = xMaxSize

UIGraphicsBeginImageContextWithOptions(contextSize, false, UIScreen.mainScreen().scale)


var imageXPlace :CGFloat = 0

for image in images {


let originX = imageXPlace
let originY = (contextSize.height - image.size.height) / 2


image.drawInRect(CGRectMake(originX, originY, image.size.width, image.size.height))

imageXPlace = imageXPlace + image.size.width
}

let combinedImage = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

return combinedImage
}


This question is also related to this one

Answer
  • Images has different sizes
  • Height of the combined image - the smallest height in UIImage

ViewController.swift

import UIKit

struct ImageToCombine {
var image = UIImage()

var size: CGSize {
    get {
        return CGSize(width: image.size.width, height: image.size.height)
    }
}

var widthAndHeightRatio: CGFloat {
    get {
        return image.size.width/image.size.height
    }
}

init (image: UIImage) {
    self.image = image
}
}

class ViewController: UIViewController {

var imageView = UIImageView()
var images = [ImageToCombine]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    imageView = UIImageView(frame: UIScreen.mainScreen().bounds)
    imageView.contentMode = .ScaleAspectFit
    view.addSubview(imageView)

    images.append(ImageToCombine(image: UIImage(named: "image1")!))
    images.append(ImageToCombine(image: UIImage(named: "image2")!))
    images.append(ImageToCombine(image: UIImage(named: "image3")!))

    imageView.image = combine(images, spaceBetweenImages: 10)
}

func combine(images: [ImageToCombine], spaceBetweenImages:CGFloat) -> UIImage {

    var fullWidth:CGFloat = 0
    var minHeight:CGFloat = images[0].size.height

    for index in 0..<images.count {
        fullWidth += images[index].size.width
        if (index+1 < images.count) {
            fullWidth += spaceBetweenImages
        }
        if (minHeight > images[index].size.height) {
            minHeight = images[index].size.height
        }
    }

    let quality = CGFloat(4)
    let boundForBetter = CGRectMake(0, 0, fullWidth*quality, minHeight*quality)

    UIGraphicsBeginImageContext(boundForBetter.size)
    let context = UIGraphicsGetCurrentContext()

    for index in 0..<images.count {

        var imageX:CGFloat = 0
        for index2 in 0..<index {
            imageX += images[index2].size.width

            if (index2+1 < images.count) {
                imageX += spaceBetweenImages
            }
        }
        let imageY:CGFloat = (minHeight - images[index].size.height)*quality/2
        let rect = CGRect(x: imageX*quality, y: imageY, width: images[index].size.width*quality, height: images[index].size.height*quality)
        CGContextDrawImage(context, rect, images[index].image.CGImage)
    }

    let combinedImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    let resultImageimage = UIImage(CGImage: imageResize(combinedImage, sizeChange:CGSize(width: fullWidth/quality, height: minHeight/quality)).CGImage!, scale: 1.0, orientation: .DownMirrored)

    return resultImageimage
}

func imageResize(imageObj:UIImage, sizeChange:CGSize)-> UIImage {

    let hasAlpha = true
    let scale: CGFloat = 0.0 // Automatically use scale factor of main screen

    UIGraphicsBeginImageContextWithOptions(sizeChange, !hasAlpha, scale)
    imageObj.drawInRect(CGRect(origin: CGPointZero, size: sizeChange))

    let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext() // !!!
    return scaledImage
}
}

result:

enter image description here

Comments