UIGraphicsGetImageFromCurrentImageContext 和 Swift 内存使用

UIGraphicsGetImageFromCurrentImageContext and Swift Memory Usage

在一个应用程序中,我们使用以下函数创建了一张组合多张图像的图像。

      UIGraphicsGetImageFromCurrentImageContext

起初我们没有注意到这个问题,因为函数 UIGraphicsGetImageFromCurrentImageContext 被多次使用。但是当应用程序完成并开始测试时,我们注意到在某些情况下应用程序会因为应用程序内存使用而崩溃。每次我们执行 UIGraphicsGetImageFromCurrentImageContext 它都会占用一些内存。在我们的应用程序的某些部分,我们不得不多次调用该函数,这是当问题浮出水面时,我们注意到了。

此外,当从循环中调用以下函数 "combineImages" 完成时,内存使用量显着下降。

您可以在下面找到导致问题的部分应用程序。

func combineImages() -> UIImage {


    let imageSize = CGSizeMake(pageWidth ,pageHeight);

    UIGraphicsBeginImageContextWithOptions(imageSize, false, 1.0);

    let rectCombine = CGRectMake( 0, 0, imageSize.width, imageSize.height)

        image1.drawInRect(rectCombine)
        image2.drawInRect(rectCombine)
        image3.drawInRect(rectCombine)
        image4.drawInRect(rectCombine)

    let combinedImage = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    return combinedImage 

  }

我们发现了那个函数

    func combineImages()

我们的内存问题几乎是无辜的。当我们试图查明有关该函数的问题时,我们更改了调用该函数的位置。

我们在将新图像添加到电影之前从电影创建循环中调用了此函数。

现在我们在电影创建循环开始之前使用 combineImages 创建图像。

当我们创建组合图像时,我们注意到内存使用量根本没有增加。

此外,减少了创建电影文件时内存使用量的增加。