2010-10-11 40 views
1

我使用以下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; 
    } 

感謝,

薩加爾

回答

0

你確定泄漏就是你說這是什麼?因爲看起來你沒有做CGRelease(newData);並因此泄漏它。此外,rawImage實例將被泄露,您需要在其上調用autorelease

+0

由於DarkDust。它解決了您的建議問題。 – 2010-10-11 13:18:15

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; 

} 
+0

由於某種原因,此方法對我崩潰「\t \t \t \t \t data [index + i] + = value;」在iOS 6設備上... – SpaceDog 2012-09-29 06:00:56