开发者

Brightness without OpenGL call

I am performing Brightness on UIImage with following src code ref. When I check this code with Instrument ,getting memory leak of 1 MB for each brightness call. I am changing brightness with slider. after debugging come to know

CFDataRef dataref=CGDataProviderCopyData(CGImageGetDataProvider(aInputImage.CGImage));

is taking a memory, can anyone help me to optimize this code.

 // ## Brightness without Open GL call

    CGImageRef Brightness(UIImage* aInputImage, float aFactor)
    {
        CGImageRef img=aInputImage.CGImage;

        CFDataRef dataref=CGDataProviderCopyData(CGImageGetDataProvider(aInputImage.CGImage));


        UInt8 *data=(UInt8 *)CFDataGetBytePtr(dataref);

        int length=CFDataGetLength(dataref);

        // Perform operation on pixels
        for(int index=0;index<length;index+=4)
        {
            // Go For BRIGHTNESS
            for(int i=0;i<3;i++)
            {
                if(data[index+i]+aFactor<0)
                {
                    data[index+i]=0;
                }
                else
                {
                    if(data[index+i]+aFactor>255)
                    {
                        data[index+i]=255;
                    }
                    else
                    {
                        data[index+i]+=aFactor;
                    }
                }
            }
        }

        // .. Take image attributes
        size_t width=CGImageGetWidth(img);
        size_t height=CGImageGetHeight(img);
        size_t bitsPerComponent=CGImageGetBitsPerComponent(img);
        size_t bitsPerPixel=CGImageGetBitsPerPixel(img);
        size_t bytesPerRow=CGImageGetBytesPerRow(img);

        // .. Do the pixel manupulation
        CGColorSpaceRef colorspace=CGImageGetColorSpace(img);
        CGBitmapInfo bitmapInfo=CGImageGetBitmapInfo(img开发者_如何学编程);
        CFDataRef newData=CFDataCreate(NULL,data,length);
        CGDataProviderRef provider=CGDataProviderCreateWithCFData(newData);

        // .. Get the Image out of this raw data
        CGImageRef newImg=CGImageCreate(width,height,bitsPerComponent,bitsPerPixel,bytesPerRow,colorspace,bitmapInfo,provider,NULL,true,kCGRenderingIntentDefault);

        // .. Prepare the image from raw data
        UIImage* rawImage = [[UIImage alloc] initWithCGImage:newImg];

        // .. done with all,so release the references
        CGImageRelease(newImg);
        CGDataProviderRelease(provider);
        CFRelease(dataref);

        return rawImage.CGImage;
    }

Thanks,

Sagar


Updated code is [ Might be useful for someone ]

// ## Brightness without OpenGL call

CGImageRef Brightness(UIImage* aInputImage, float aFactor)
{
    CGImageRef img=aInputImage.CGImage;

    CFDataRef dataref=CGDataProviderCopyData(CGImageGetDataProvider(aInputImage.CGImage));

    int length=CFDataGetLength(dataref);
    UInt8 *data=(UInt8 *)CFDataGetBytePtr(dataref);

    // Perform operation on pixels
    for(int index=0;index<length;index+=4)
    {
        // Go For BRIGHTNESS
        for(int i=0;i<3;i++)
        {
            if(data[index+i]+aFactor<0)
            {
                data[index+i]=0;
            }
            else
            {
                if(data[index+i]+aFactor>255)
                {
                    data[index+i]=255;
                }
                else
                {
                    data[index+i]+=aFactor;
                }
            }
        }
    }

    // .. Take image attributes
    size_t width=CGImageGetWidth(img);
    size_t height=CGImageGetHeight(img);
    size_t bitsPerComponent=CGImageGetBitsPerComponent(img);
    size_t bitsPerPixel=CGImageGetBitsPerPixel(img);
    size_t bytesPerRow=CGImageGetBytesPerRow(img);

    // .. Do the pixel manupulation
    CGColorSpaceRef colorspace=CGImageGetColorSpace(img);
    CGBitmapInfo bitmapInfo=CGImageGetBitmapInfo(img);
    CFDataRef newData=CFDataCreate(NULL,data,length);
    CGDataProviderRef provider=CGDataProviderCreateWithCFData(newData);

    // .. Get the Image out of this raw data
    CGImageRef newImg=CGImageCreate(width,height,bitsPerComponent,bitsPerPixel,bytesPerRow,colorspace,bitmapInfo,provider,NULL,true,kCGRenderingIntentDefault);

    // .. Prepare the image from raw data
    UIImage* rawImage = [[[UIImage alloc] initWithCGImage:newImg] autorelease];

    // .. done with all,so release the references
    CFRelease(newData);
    CGImageRelease(newImg);
    CGDataProviderRelease(provider);
    CFRelease(dataref);

    return rawImage.CGImage;

}


Are you sure the leak is where you're saying it is ? Because it seems you didn't do CGRelease(newData); and thus leaking it. Also, the rawImage instance will be leaked, you need to call autorelease on it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜