I have a UISlider which increases the brightness of a UIImage as its value increases. But when I want to reduce the brightness and bring back the UISlider to the value that holds the original image, The brightness of the image does not reduce and it remains the same as it was on the last value of the UISlider.
The code is below
Slider1 = [[UISlider alloc]initWithFrame:CGRectMake(100, 350, 100, 100)];
[Slider1 addTarget:self action:@selector(Slider1Action:) forControlEvents:UIControlEventValueChanged];
Slider1.minimumValue = 0.0;
//slider.maximumValue = 50.0;
Slider1.continuous = YES;
[self.view addSubview:Slider1];
-(void)Slider1Action:(id)sender{
CGImageRef inImage = imageCollage.image.CGImage;
CFDataRef ref = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
UInt8 * buf = (UInt8 *) CFDataGetBytePtr(ref);
int length = CFDataGetLength(ref);
float value2 = (1+Slider1.value);
for(int i=0; i<length; i+=4)
{
int r = i;
int g = i+1;
int b = i+2;
int red = buf[r];
int green = buf[g];
int blue = buf[b];
buf[r] = SAFECOLOR(red*value2);
buf[g] = SAFECOLOR(green*value2);
buf[b] = SAFECOLOR(blue*value2);
}
CGContextRef ctx = CGBitmapContextCreate(buf,
CGImageGetWidth(inImage),
CGImageGetHeight(inImage),
CGImageGetBitsPerComponent(inImage),
CGImageGetBytesPerRow(inImage),
CGImageGetColorSpace(inImage),
CGImageGetAlphaInfo(inImage));
CGImageRef img = CGBitmapContextCreateImage(ctx);
[imageCollage setImage:[UIImage imageWithCGImage:img]];
CFRelease(ref);
CGContextRelease(ctx);
CGImageRelease(img);}
How can I reduce the brightness of the UIImage with the UISlider so that I can get the original image back again ?
Your help is much appreciated.
P.S.- I do not want to use Apple’s OpenGL for this .
You’re destructively setting the values of the pixels in your image. You are taking the input image, changing the pixel brightness and then using that output as your new input the next time around. Once you have brightened it up, you can’t get that information back again when you slide it down.
You need to keep the original image as the source and apply the changes to a copy of that image and then display that copy.