内存泄漏UIIMageFromCVMat [英] Memory Leak UIIMageFromCVMat

查看:289
本文介绍了内存泄漏UIIMageFromCVMat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将CVMat转换为UIImage,但是我遇到了内存泄漏,并且无法在我的生活中弄清楚在哪里。问题在于。我正在使用openCV在其示例代码中使用的相同功能位于这里

I am trying to convert a CVMat into a UIImage, but am running into a memory leak and cannot for the life of me figure out where. the issue lies. I am using the same function that openCV uses in their sample code Located Here.

代码说明如下:

+(UIImage *)UIImageFromCVMat:(cv::Mat)cvMat{
    NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()];
    CGColorSpaceRef colorSpace;

    if (cvMat.elemSize() == 1) {
        colorSpace = CGColorSpaceCreateDeviceGray();
    } else {
        colorSpace = CGColorSpaceCreateDeviceRGB();
    }

    CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
    bool alpha = cvMat.channels() == 4;
    CGBitmapInfo bitMapInfo = (alpha ? kCGImageAlphaLast : kCGImageAlphaNone) | kCGBitmapByteOrderDefault;
    // Creating CGImage from cv::Mat
    CGImageRef imageRef = CGImageCreate(cvMat.cols,                                 //width
                                        cvMat.rows,                                 //height
                                        8,                                          //bits per component
                                        8 * cvMat.elemSize(),                       //bits per pixel
                                        cvMat.step[0],                            //bytesPerRow
                                        colorSpace,                                 //colorspace
                                        bitMapInfo,                                 // bitmap info
                                        provider,                                   //CGDataProviderRef
                                        NULL,                                       //decode
                                        false,                                      //should interpolate
                                        kCGRenderingIntentDefault                   //intent
                                        );


    // Getting UIImage from CGImage
    UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpace);

    return finalImage; 
}

有人对问题的位置有任何建议吗? (我们已经缩小并确定这是泄漏的地方)。

Does anybody have any suggestions on where the issue could be? (We have narrowed down and know for certain that this is the place with the leak).

编辑

提供我发现的更多信息:如果从未使用过该函数的输出,则没有问题。只有当我们将这个结果分配给我们遇到问题的不同类中的变量时。

To give some more information that I have found: If the output of the function is never used, there are no issues. It is only if we assign this result to a variable within a different class that we run into issues.

推荐答案

好的,经过多次搜索,已经发现了解决方案(虽然我仍然没有对问题有一个非常深刻的理解,如果有人可以指出我正确的方向,为什么它是一个问题,这将是美好的)。

Okay, after much searching, the solution has been discovered (though I still do not have a very deep understanding of the problem, and if somebody could point me in the right direction for the why it was an issue, that'd be wonderful).

所有需要做的就是在 @autoreleasepool {} tag。

All that needed to be done was to wrap the lines of code that used the result of this in a @autoreleasepool{} tag.

我从这个问题

这篇关于内存泄漏UIIMageFromCVMat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆