当我在一个 UIImage 中组合许多大图像时,空白 UIImage

Blank UIImage when I combined many large images in one UIImage

我想将多张图片垂直合并到一张图片中。 我的图像具有相同的尺寸 1500x2122。

我的问题是,根据要合并的图像数量,最终图像是空白的。 有7张图片,最后的图片就ok了。 用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!
    }
}

在我的控制器中,下面的函数被添加到 ViewDidLoad 以便在视图中添加最终的 UIImage

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)
}

当最终图像为空白时,我在控制台中没有错误。

我确定这是尺寸问题。我尝试添加 "smaller" 张图片 (1500x1190),但我可以合并的图片数量限制高于我使用大图片时的图片数量。

UIGraphicsGetImageFromCurrentImageContext大小有限制吗?

编辑 1

Memory gauge 截屏视频。 "Peaks" 是在调用 addImageStrip_v2 时。

谢谢

这是内存问题。下面的代码解决了它:

for i in 1..<self.imagesStrip.count{ autoreleasepool {