Merge Two Image on to one Image programmatically in iphone

前端 未结 10 820
旧巷少年郎
旧巷少年郎 2020-12-04 17:01

Hello I am developing the application which requires to merge two Image , My image size are 320*240 by merging both Image I want the size to be 320 * 480 . How can i do this

10条回答
  •  抹茶落季
    2020-12-04 17:43

    This implementation works for variadic arguments, so you can pass an unlimited number of images to merge vertically:

    public static func mergeVertically(images: UIImage...) -> UIImage? {
        let maxWidth = images.reduce(0.0) { max($0, $1.size.width) }
        let totalHeight = images.reduce(0.0) { $0 + $1.size.height }
    
        UIGraphicsBeginImageContextWithOptions(CGSize(width: maxWidth, height: totalHeight), false, 0.0)
        defer {
            UIGraphicsEndImageContext()
        }
    
        let _ = images.reduce(CGFloat(0.0)) {
            $1.draw(in: CGRect(origin: CGPoint(x: 0.0, y: $0), size: $1.size))
            return $0 + $1.size.height
        }
    
        return UIGraphicsGetImageFromCurrentImageContext()
    }
    

提交回复
热议问题