3

I'm trying to fetch an image of PDF page and edit it. Everything works fine, but there is an huge memory growth. Profiler says that there is no any memory leak. Also profiler says that 90% memory allocated at UIGraphicsGetCurrentContext() and UIGraphicsGetImageFromCurrentImageContext(). This code not runs in the loop and there is no need to wrap it with @autorelease.

        if ((pageRotation == 0) || (pageRotation == 180) ||(pageRotation == -180)) {
              UIGraphicsBeginImageContextWithOptions(cropBox.size, NO, PAGE_QUALITY);
        }
        else {
            UIGraphicsBeginImageContextWithOptions(
                                                   CGSizeMake(cropBox.size.height, cropBox.size.width), NO, PAGE_QUALITY);
        }


        CGContextRef imageContext = UIGraphicsGetCurrentContext();
        [PDFPageRenderer renderPage:_PDFPageRef inContext:imageContext pagePoint:CGPointMake(0, 0)];
        UIImage *pageImage = UIGraphicsGetImageFromCurrentImageContext();

        [[NSNotificationCenter defaultCenter] postNotificationName:@"PAGE_IMAGE_FETCHED" object:pageImage];
        UIGraphicsEndImageContext();

But debug shows that the memory growing occurs only when I start editing the fetched image. For image editing I use the Leptonica library. For example:

+(void) testAction:(UIImage *) image{
    PIX * pix =[self getPixFromUIImage:image];
    pixConvertTo8(pix, FALSE);
    pixDestroy(&pix);
}

Before pixConvertTo8 app takes 13MB, after - 50MB. Obviously growth depends on image size. Converting method:

+(PIX *) getPixFromUIImage:(UIImage *) image{
    CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
    UInt8 const* pData = (UInt8*)CFDataGetBytePtr(data);
    Pix *myPix = (Pix *) malloc(sizeof(Pix));
    CGImageRef myCGImage = [image CGImage];
    myPix->w = CGImageGetWidth (myCGImage)-1;
    myPix->h = CGImageGetHeight (myCGImage);
    myPix->d = CGImageGetBitsPerPixel([image CGImage]) ;
    myPix->wpl = CGImageGetBytesPerRow (myCGImage)/4 ;
    myPix->data = (l_uint32 *)pData;
    myPix->colormap = NULL;
    myPix->text="text";
    CFRelease(data);

    return myPix;
}

P.S. Sorry for my terrible English.

free334
  • 51
  • 5

0 Answers0