Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QCAR::Image to UIImage - CoreGraphics and retain crash

I'm using Vuforia , trying to get pixels from camera instance and convert to UIImage

- (UIImage *)createUIImage:(const QCAR::Image *)qcarImage
{
    int width = qcarImage->getWidth();
    int height = qcarImage->getHeight();
    int bitsPerComponent = 8;
    int bitsPerPixel = QCAR::getBitsPerPixel(QCAR::RGB888);
    int bytesPerRow = qcarImage->getBufferWidth() * bitsPerPixel / bitsPerComponent;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaNone;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, qcarImage->getPixels(), QCAR::getBufferSize(width, height, QCAR::RGB888), NULL);

    CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
    CGImageRef imageRefRetain = CGImageRetain(imageRef);
    UIImage *image = [UIImage imageWithCGImage:imageRefRetain];

    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpaceRef);
    CGImageRelease(imageRef);

    return image;
}

After getting the image, I add it to a mutable array.

Once retrieving the image from the mutable array, I get crash on every method i.e.

UIImageView setImage: 

or even calling

NSData* comppressedData = UIImageJPEGRepresentation(image,1);

The only way to make my app not to crash is adding the following

- (UIImage *)createUIImage:(const QCAR::Image *)qcarImage
{
    ...
    UIImage *image = [UIImage imageWithCGImage:imageRefRetain];

    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpaceRef);
    CGImageRelease(imageRef);

    NSData* comppressedData = UIImageJPEGRepresentation(image,1);
                    UIImage* jpegImage = [UIImage imageWithData:comppressedData];
    return jpegImage;
}

So i think it's an issue related to the CGImageRef getting released. I really need to understand what's going on and why the image ref isn't valid after existing the method.

like image 989
ibm123 Avatar asked Nov 29 '25 18:11

ibm123


1 Answers

The problem is that the qcarImage is going out of scope and taking the raw image data with it. Copy the image buffer and provide that to the CGDataProviderRef

NSData * imageData = [NSData dataWithBytes:qcarImage->getPixels() 
                                    length:QCAR::getBufferSize(width, height, QCAR::RGB888)];
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, imageData.bytes, imageData.length, NULL);
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
//This was leaking memory, commenting out
//CGImageRef imageRefRetain = CGImageRetain(imageRef);
UIImage *image = [UIImage imageWithCGImage:imageRef];
like image 196
Fruity Geek Avatar answered Dec 01 '25 21:12

Fruity Geek