开发者

Setting the contrast of an image in iPhone

I'm using the following code for setting the contrast of an image based on the slider value. The slider range is from 0.0f to 2.0f. Its runnig fine in simulator but its crashing on device due to low memory. Can any one help me what's wrong in this code.

Thanks in advance....

-(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开发者_如何学Python)*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);
}


You might have some memory leaks.

Any function that is CF...Create() will need to have corresponding CFRelease() called on it. The following has no release:

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

I think you need to clean up after copying as well:

CFDataRef dataref=CopyImagePixels(img);

You cleaned up after newImg okay. Can't see any other leaks but check your Create/Copying that you clean up the memory afterwards.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜