我使用以下src代碼參考在UIImage上執行亮度。當我用Instrument檢查這個代碼時,每次亮度調用都會產生1 MB的內存泄漏。我正在改變滑塊的亮度。調試後知道了嗎沒有OpenGL調用的亮度
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;
}
感謝,
薩加爾
由於DarkDust。它解決了您的建議問題。 – 2010-10-11 13:18:15