Is there a way to apply brightness to uiimages. For example I have a UIImageview and inside that i have a uiimage. I want to manipulate its brightness with a help of UISlider wi
Have a look at Apple's demo app GLImageProcessing , it's really fast:
here is the code:-
CGImageRef inImage = currentImage.CGImage;
CFDataRef ref = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
UInt8 * buf = (UInt8 *) CFDataGetBytePtr(ref);
int length = CFDataGetLength(ref);
float value2 = (1+slider.value-0.5);
NSLog(@"%i",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);
[photoEditView setImage:[UIImage imageWithCGImage:img]];
CFRelease(ref);
CGContextRelease(ctx);
CGImageRelease(img);
define this at the top:-
#define SAFECOLOR(color) MIN(255,MAX(0,color))
Open Gl to UIImage Create
-(UIImage *) glToUIImage {
NSInteger myDataLength = 320 * 480 * 4;
// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
// gl renders "upside down" so swap top to bottom into new array.
// there's gotta be a better way, but this works.
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
for(int y = 0; y <480; y++)
{
for(int x = 0; x <320 * 4; x++)
{
buffer2[(479 - y) * 320 * 4 + x] = buffer[y * 4 * 320 + x];
}
}
// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(
NULL, buffer2, myDataLength, NULL);
// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * 320;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGImageAlphaNone;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
// make the cgimage
CGImageRef imageRef = CGImageCreate(320, 480, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
// then make the uiimage from that
UIImage *myImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
//UIImageWriteToSavedPhotosAlbum(myImage,nil,nil,nil);
[[NSUserDefaults standardUserDefaults] setObject:UIImageJPEGRepresentation(myImage,1) forKey:@"image"];
return myImage;
}