使用UIImageView内存泄漏崩溃显示帧中的帧流

时间:2012-02-24 14:11:42

标签: ios memory-leaks uiimage vuforia

我正在使用QCAR库,每帧都从库中获取相机帧。

我想在我的setImage上使用UIImageView* mCurFrameView来展示此框架。这首先起作用,我能够看到帧运行平稳,但是20秒后它会崩溃。

有时我会在

上获得EXC_BAD_ACCESS
int retVal = UIApplicationMain(argc, argv, nil, nil); 

有时它只是gdb并暂停。

有时在他崩溃之前我得到了

2012-02-24 15:59:15.726 QRAR_nextGen[226:707] Received memory warning.

这是我的代码:

-(void)SaveCurrentFrame:(UIImage*)image
{

        mCurFrameView.image = image;

}


- (void)renderFrameQCAR
{

   cout<<"I am starting"<<endl;


QCAR::State state = QCAR::Renderer::getInstance().begin();


QCAR::setFrameFormat(QCAR::RGB888, true);

const QCAR::Image *image = state.getFrame().getImage(1);// 0: YUV, 1: Grayscale image

if (image)
{
    const char *data = (const char *)image->getPixels();
    int width = image->getWidth(); int height = image->getHeight();

    colorSpace = CGColorSpaceCreateDeviceRGB();
    bitmapInfo = kCGBitmapByteOrderDefault;
    provider = CGDataProviderCreateWithData(NULL, data, width*height*3, NULL);
    intent = kCGRenderingIntentDefault;
    imageRef = CGImageCreate(width, height, 8, 8*3, width * 3, colorSpace, bitmapInfo,    provider, NULL, NO, intent);

    mCurFrame = [UIImage imageWithCGImage:imageRef];


    cout<<"I am waiting"<<endl;
   [self performSelectorOnMainThread:@selector(SaveCurrentFrame:) withObject:mCurFrame waitUntilDone:YES];

}

我尝试了几件事:显示CALayer以显示相机,释放,保留,自动释放,定义和不定义属性和合成。

如果有人可以帮助我,我会非常感激。我正在失去理智。非常感谢。

3 个答案:

答案 0 :(得分:0)

您没有释放(泄漏)每个CGDataProvider,这意味着您将所有图像数据存储在内存中。尝试在performSelectorOnMainThread之后执行CGDataProviderRelease(provider)。

答案 1 :(得分:0)

也许你错过了这个:

   QCAR::Renderer::getInstance().end();

这是一个适用于iPhone 4S / 5 iOS 6的代码片段

- (void)renderFrameIntoImage {
   QCAR::State state = QCAR::Renderer::getInstance().begin();
   QCAR::setFrameFormat(QCAR::GRAYSCALE, true);
   const QCAR::Image *image = state.getFrame().getImage(0); // 0: YUV, 1: Grayscale image
   const char *data = (const char *)image->getPixels();
   int width = image->getWidth(); int height = image->getHeight();
   CGColorSpace *colorSpace = CGColorSpaceCreateDeviceGray();
   CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
   CGDataProvider *provider = CGDataProviderCreateWithData(NULL, data, width*height, NULL);
   CGColorRenderingIntent intent = kCGRenderingIntentDefault;
   CGImageRef imageRef = CGImageCreate(width, height, 8, 8, width * 1, colorSpace, bitmapInfo, provider, NULL, NO, intent);
   UIImage *myImage = [UIImage imageWithCGImage:imageRef];
   QCAR::Renderer::getInstance().end();
   UIImageWriteToSavedPhotosAlbum(myImage, nil, nil, NULL);
}

...问候

答案 2 :(得分:0)

你有三次泄密。

您必须为每个CoreFoundation方法调用一个Release方法,其中包含Create个字词。

  1. CGColorSpaceRelease(colorSpace);
  2. CGDataProviderRelease(provider);
  3. CGImageRelease(imageRef);
  4. 用于执行QCAR :: Image到UIImage转换的C ++方法

    inline UIImage *imageWithQCARCameraImage(const QCAR::Image *cameraImage)
    {
        UIImage *image = nil;
    
        if (cameraImage) {
            CGColorSpaceRef colorSpace = NULL;
            QCAR::PIXEL_FORMAT pixelFormat = cameraImage->getFormat();
    
            int bitsPerPixel = QCAR::getBitsPerPixel(pixelFormat);
    
            switch (pixelFormat) {
                case QCAR::RGB888:
                    colorSpace = CGColorSpaceCreateDeviceRGB();
                    break;
                case QCAR::GRAYSCALE:
                    colorSpace = CGColorSpaceCreateDeviceGray();
                    break;
                case QCAR::YUV:
                case QCAR::RGB565:
                case QCAR::RGBA8888:
                case QCAR::INDEXED:
                    std::cerr << "Image format conversion not implemented." << std::endl;
                    break;
                case QCAR::UNKNOWN_FORMAT:
                    std::cerr << "Image format unknown." << std::endl;
                    break;
            }
    
            float width = cameraImage->getWidth();
            float height = cameraImage->getHeight();
            int bytesPerRow = cameraImage->getStride();
            const void *baseAddress = cameraImage->getPixels();
            size_t totalBytes = QCAR::getBufferSize(width, height, pixelFormat);
    
            if (bitsPerPixel > 0 && colorSpace != NULL) {
                CGDataProviderRef provider = CGDataProviderCreateWithData(NULL,
                                                                          baseAddress,
                                                                          totalBytes,
                                                                          NULL);
    
                int bitsPerComponent = 8;
                CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaNone;
                CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
                CGImageRef imageRef = CGImageCreate(width,
                                                    height,
                                                    bitsPerComponent,
                                                    bitsPerPixel,
                                                    bytesPerRow,
                                                    colorSpace,
                                                    bitmapInfo,
                                                    provider,
                                                    NULL,
                                                    NO,
                                                    renderingIntent);
    
                image = [UIImage imageWithCGImage:imageRef];
                CGColorSpaceRelease(colorSpace);
                CGDataProviderRelease(provider);
                CGImageRelease(imageRef);
            }
        }
    
        return image;
    }