UIGraphicsGetImageFromCurrentImageContext和Swift Memory Usage

时间:2016-01-11 08:44:29

标签: ios swift memory uiimage

在一个应用程序中,我们使用以下功能创建一个组合多个图像的图像。

      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 

  }

1 个答案:

答案 0 :(得分:0)

我们发现了这个功能

    func combineImages()

对我们的记忆问题几乎是无辜的。当我们试图查明有关该函数的问题时,我们会改变调用该函数的位置。

在将新图像添加到电影之前,我们正在通过电影创建循环调用此函数。

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

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

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

相关问题