I have a UIImage of size 1080x1920, and a UIView of size 375x812. I want to scale up the view and draw it over the UIImage in the center, in this way:
This is the code I implemented to get the UIView over the UIImage scaling it up:
let proportionalWidthUpperView = (inImage.size.height * self.view.frame.size.width)/self.view.frame.size.height
let differenceWidth = inImage.size.width - proportionalWidthUpperView
UIGraphicsBeginImageContext(inImage.size)
let rect = CGRect(x: differenceWidth/2, y: 0, width: inImage.size.width-(differenceWidth), height: inImage.size.height)
inImage.draw(in: CGRect(x: 0, y: 0, width: inImage.size.width, height: inImage.size.height))
imageFromUpperView?.draw(in: rect)
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return result!
Scaling the view up making It match the UIImage height, I retrieve the difference in width and I add it to the left and right of the rect.
This method works perfectly on iPhones but using it on iPads with bigger view frame I get the view scaled it up, do you know what I'm doing wrong?
Try to add UIView as subview to UIImageView and you can apply transformations and scaling and rest the frame of UIView added.
Hope this will simplify and help.