【发布时间】:2018-02-10 14:48:18
【问题描述】:
我有多种尺寸的帧,可以硬编码,或者由服务器决定。我必须从 Gallery 中选择 Image,它肯定可以是多个维度的。
- 我正在从图库中选择图片
-
我正在使用代码生成白色背景 UIImage。
让 size = CGSize(宽度: 424/2, 高度: 664/2)
UIGraphicsBeginImageContextWithOptions(size, true, 0) UIColor.white.setFill() UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height)) let background_image: UIImage? = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext()
现在,我想要制作另一个图像,它保持领先 20 像素、前 20 像素以及宽度和高度比原始背景小 20 像素。
我怎样才能实现它。
在来到 StackOverflow 之前我尝试过的事情。
func mergedImageWith(frontImage:UIImage?, backgroundImage: UIImage?) -> UIImage{
if (backgroundImage == nil) {
return frontImage!
}
let size = CGSize(width: 424/2, height: 664/2)
UIGraphicsBeginImageContextWithOptions(size, true, 0)
UIColor.white.setFill()
UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height))
let backgroundImage2: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
backgroundImage2?.draw(in: CGRect.init(x: 0, y: 0, width: size.width, height: size.height))
frontImage?.draw(in: getAspectFillFrame(sizeImageView: size2, sizeImage: (frontImage?.size)!))
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
这里的背景图像是使用方面填充创建的,但问题是起始位置以及完整的高度和宽度。
用非常简单的话来说。这就像制作自定义框架并将它们与图像(方面填充)合并以进行打印。
谁能帮帮我
谢谢。
【问题讨论】:
标签: ios uiimage uigraphicscontext