我想将多个垂直图像合并到一张图像中。 我的图像具有相同的尺寸 1500x2122。
我的问题是,根据要组合的图像数量,最终图像是空白的。 有7张图像,最终图像就可以了。 使用 8 时,最终图像是空白的。
extension UIImage {
class func combineTopToBottom(image1: UIImage, withImage image2: UIImage) -> UIImage {
let size:CGSize = CGSize(width: image1.size.width, height: image1.size.height + image2.size.height)
UIGraphicsBeginImageContext(size);
image1.draw(in: CGRect(x: 0, y: 0, width: size.width, height: image1.size.height))
image2.draw(in: CGRect(x: 0, y: image1.size.height, width: size.width, height: image2.size.height))
let finalImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return finalImage!
}
}
在我的 Controller 中,以下功能被添加到 ViewDidLoad
为了添加最后的 UIImage
在 View 中:
func addImageStrip_v2() {
var newCombinedImage:UIImage! = UIImage(data: self.imagesStrip[1].image)
for i in 1..<self.imagesStrip.count{
newCombinedImage = UIImage.combineTopToBottom(image1: newCombinedImage!, withImage: UIImage(data: imagesStrip[i].image)!)
}
self.imageView.image = newCombinedImage
self.imageView.addRatioConstraint()
}
func addRatioConstraint() {
let aspectRatioImageView:NSLayoutConstraint = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: self, attribute: .width, multiplier: self.image!.size.height/self.image!.size.width, constant: 1)
self.addConstraint(aspectRatioImageView)
}
当最终图像为空白时,控制台中没有错误。
我确定这是一个尺寸问题。我尝试添加“较小”图像 (1500x1190),但可以组合的图像数量限制高于使用大图像时的数量。
是UIGraphicsGetImageFromCurrentImageContext
大小有限?
编辑1
Memory gauge截屏视频。 “峰值”是指 addImageStrip_v2
时叫做。
谢谢
最佳答案
这是一个内存问题。下面的代码解决了这个问题:
for i in 1..<self.imagesStrip.count{ autoreleasepool {
关于ios - 当我将许多大图像合并到一个 UIImage 中时,UIImage 为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57037283/