代码添加到Github https://github.com/hemangshah/MergeImages让您了解真正的问题。
这是层次结构:
-- ViewController.View P [width: 375, height: 667]
---- UIImageView A [width: 375, height: 667] Name: imgBackground
[A is holding an image of size(1287,1662)]
---- UIImageView B [width: 100, height: 100] Name: imgForeground
[B is holding an image of size(2400,982)]
我试图将 A 与 B 合并,但结果被拉伸。
这是合并代码:
func mixImagesWith(frontImage:UIImage?, backgroundImage: UIImage?, atPoint point:CGPoint, ofSize signatureSize:CGSize) -> UIImage {
let size = self.imgBackground.frame.size
UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
backgroundImage?.draw(in: CGRect.init(x: 0, y: 0, width: size.width, height: size.height))
frontImage?.draw(in: CGRect.init(x: point.x, y: point.y, width: signatureSize.width, height: signatureSize.height))
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
Note:
- .contentMode = .scaleAspectFit
- 代码可以工作,但结果被拉伸。
- 请参阅代码中的这一行,
let size = self.imgBackground.frame.size
– 我需要改变这个来解决问题。查找子视图相对于 UIImage 大小的原点 https://stackoverflow.com/questions/43889254/find-the-origin-of-subview-with-respect-to-uiimage-size
这是了解问题的屏幕截图:
我应该怎么做才能获得合并函数的正确输出?