微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

UIGraphicsGetImageFromCurrentImageContext 不生成图像并返回 nil

如何解决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 举报,一经查实,本站将立刻删除。