ios - 当我将许多大图像合并到一个 UIImage 中时,UIImage 为空白

标签 ios swift uiimage

我想将多个垂直图像合并到一张图像中。 我的图像具有相同的尺寸 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/

相关文章:

ios - 选择器 'touchesBegan:withEvent:' 的覆盖方法具有不兼容的类型 '(NSSet, UIEvent) -> ()'

ios - 如何获取所有用户的特定信息?

ios - SwiftUI:为特定 View 设置状态栏颜色

ios - DropboxSDK : unable to link error

ios - 在图像上渲染文本,以全分辨率保存图像?

ios - 从 Storyboard 设置时看不到 UIImageView 图像

ios - UINavigatoinBar 背景图像 swift 出现两次

arrays - 以编程方式为 DSP Split Complex 复数创建存储阵列

ios - swift 链接器命令失败,退出代码为 1 Google Maps iOS Util pod install

ios - 合并并编辑2个UIImage