Created
November 4, 2019 06:53
-
-
Save aminbenarieb/607a03e7407e773f5549e32353d5ca6b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment