Reputation: 45
I have a single image. And I have a collection view with banner images. Now,I need to combine these 2 images into single image without affecting their quality and height so that I could be able to download the merged image. I searched but couldn't find proper solutions for swift 3. My code is given as:
Upvotes: 2
Views: 4140
Reputation: 1318
As per as your question You have to add two images and show up in a single UIImageView.
Here is a simple example of adding two images vertically and showing up in an 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
I hope it will help you to start with.
Upvotes: 3