没有OpenGL调用的亮度

时间:2010-10-11 04:47:30

标签: iphone

我正在使用以下src代码ref在UIImage上执行Brightness。当我使用Instrument检查此代码时,每次亮度调用都会导致1 MB的内存泄漏。我用滑块改变亮度。经过调试后才知道

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

正在记忆,任何人都可以帮我优化这段代码。

 // ## 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;
    }

谢谢,

Sagar的

2 个答案:

答案 0 :(得分:1)

更新的代码是[可能对某人有用]

// ## 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;

}

答案 1 :(得分:0)

你确定泄漏是你所说的吗?因为看起来你没有做CGRelease(newData);从而泄露它。此外,rawImage实例将被泄露,您需要在其上调用autorelease