UIGraphicsGetImageFromCurrentImageContext 不生成图像并返回 nil

问题描述

我有 var drawAnimationObjectsByIndex: [Int : DrawAnimationModel] = [:] 并且 DrawAnimationModel 有多个 CAShapeLayer 并按顺序排列它们并添加到父层中。帧索引是保留DrawAnimationModel以便通过UIGraphicsGetimageFromCurrentimageContext生成图像并将图像按顺序放入AssetWriter以创建电影文件(.mov)。该过程一直工作到特定的帧索引 - 通常是 ~140 中的 91/92 并且 UIGraphicsGetimageFromCurrentimageContext 为所有其余的 calayers 返回 nil。我认为这是内存问题并设置了 autoreleasepool 但它似乎不起作用。我认为上下文可能很忙,所以我重用了相同维度的上下文并绘制了 calayers 并生成了图像,但没有超过 92。我们如何通过 UIGraphicsGetimageFromCurrentimageContext 获得有保证的图像生成?这是代码 -

     autoreleasepool {
            UIGraphicsBeginImageContextWithOptions(settings.size,false,UIScreen.main.scale)
                guard let context = UIGraphicsGetCurrentContext() else {
                    fatalError()
                }

                var image : UIImage? = nil
                for index in 0...drawAnimationObjectsByIndex.count  {
                    if let object = drawAnimationObjectsByIndex[index] {
                        object.parent.render(in: context)
                        
                        image = UIGraphicsGetimageFromCurrentimageContext()
                        
                        if let image = image {
                            images.append(image)
                        }
                        else {
                            os_log("the image is not generated at %d,appending the last image.",log: self.tag,type: .debug,index)
                            images.append(images.last!)
                        }
                        image = nil
                        drawAnimationObjectsByIndex[index] = nil
                    }
                }
            UIGraphicsEndImageContext()
        }

这是日志-

2021-05-10 05:34:35.705554-0500 apate[5116:2547215] [DrawingRender] is initialized : initialized
2021-05-10 05:34:35.705585-0500 apate[5116:2547215] [DrawingRender] recordingData.shapesByFrameIndex.count : 173
2021-05-10 05:34:37.892562-0500 apate[5116:2547215] [DrawingRender] the image is not generated at 92,appending the last image.
2021-05-10 05:34:37.897744-0500 apate[5116:2547215] [DrawingRender] the image is not generated at 93,appending the last image.
2021-05-10 05:34:37.901005-0500 apate[5116:2547215] [DrawingRender] the image is not generated at 94,appending the last image.

继续......

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)