读取CGImage数据后内存泄漏

时间:2013-11-11 22:09:34

标签: ios memory-leaks

我试图理解为什么每次在我的级别加载路由中调用以下代码块时,它都会根据XCode的5.0.1内存监视器泄漏内存。这段代码用于根据alpha chanel检查图像的实际边界。

        UIImageView* iv = (UIImageView*)m_image->GetId();

        // First get the image into your data buffer
        CGImageRef image = [iv.image CGImage];
        int width = CGImageGetWidth(image);
        int height = CGImageGetHeight(image);
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        void* rawData = malloc(height * width * 4);
        int bytesPerPixel = 4;
        int bytesPerRow = bytesPerPixel * width;
        int bitsPerComponent = 8;
        CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
        CGColorSpaceRelease(colorSpace);

       CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
       CGContextRelease(context);

       // rawData contains the image data in the RGBA8888 pixel format.
       int lx=99999999,bx=0,ly=99999999,by=0;
       for( int x=0; x<width; x++ )
       {
           for( int y=0; y<height; y++ )
           {
                int pixelInfo = (bytesPerRow * y) + x * bytesPerPixel; // data is a png
                unsigned char* d = (unsigned char*)rawData;
                if( d[pixelInfo + 3] ) // check alpha
                {
                     if( x < lx ) lx = x;
                     if( x > bx ) bx = x;
                     if( y < ly ) ly = y;
                     if( y > by ) by = y;
                }
           }
       }
       free(rawData);

如果你们有更好的方法解决这个问题,那么请指教,但是这个实现很有效,因为它泄漏了。

感谢。

0 个答案:

没有答案