在iPhone中设置图像的对比度

时间:2010-08-30 05:22:53

标签: iphone image-processing

我正在使用以下代码根据滑块值设置图像的对比度。滑块范围从0.0f到2.0f。它在模拟器中运行良好,但由于内存不足而导致设备崩溃。任何人都可以帮我解决这段代码中的错误。

提前致谢....

-(void)contrast:(float)value{
CGImageRef img=refImage.CGImage;
CFDataRef dataref=CopyImagePixels(img);
UInt8 *data=(UInt8 *)CFDataGetBytePtr(dataref);
int length=CFDataGetLength(dataref);
for(int index=0;index<length;index+=4){

    int alphaCount = data[index+0];
    int redCount = data[index+1];
    int greenCount = data[index+2];
    int blueCount = data[index+3];

    alphaCount = ((alphaCount-128)*value ) + 128;
    if (alphaCount < 0) alphaCount = 0; if (alphaCount>255) alphaCount =255;
    data[index+0] = (Byte) alphaCount;

    redCount = ((redCount-128)*value ) + 128;
    if (redCount < 0) redCount = 0; if (redCount>255) redCount =255;
    data[index+1] = (Byte) redCount;

    greenCount = ((greenCount-128)*value ) + 128;
    if (greenCount < 0) greenCount = 0; if (greenCount>255) greenCount =255;
    data[index+2] = (Byte) greenCount;

    blueCount = ((blueCount-128)*value ) + 128;
    if (blueCount < 0) blueCount = 0; if (blueCount>255) blueCount =255;
    data[index+3] = (Byte) blueCount;       
}
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);
CGColorSpaceRef colorspace=CGImageGetColorSpace(img);
CGBitmapInfo bitmapInfo=CGImageGetBitmapInfo(img);
CFDataRef newData=CFDataCreate(NULL,data,length);
CGDataProviderRef provider=CGDataProviderCreateWithCFData(newData);
CGImageRef newImg=CGImageCreate(width,height,bitsPerComponent,bitsPerPixel,bytesPerRow,colorspace,bitmapInfo,provider,NULL,true,kCGRenderingIntentDefault);
[ImgView setImage:[UIImage imageWithCGImage:newImg]];
CGImageRelease(newImg);
CGDataProviderRelease(provider);
}

1 个答案:

答案 0 :(得分:2)

您可能有一些内存泄漏。

任何CF的函数... Create()都需要调用相应的CFRelease()。以下没有发布:

CFDataRef newData=CFDataCreate(NULL,data,length);

我认为你也需要在复制后进行清理:

CFDataRef dataref=CopyImagePixels(img);

你在newImg之后清理好了。看不到任何其他泄漏,但检查你的创建/复制,然后你清理内存。