在iPhone中使用uislider处理图像亮度

时间:2012-01-17 11:11:05

标签: iphone ios ios4

我已经实现了在iPhone中使用uislider更改图像亮度而不使用openGlImageProcessing。我想改变图像的特定区域的亮度而不是整个图像,仅应该使图像的特定区域变得明亮,例如图像的中心区域应该变得明亮,或者整个图像中的特定颜色应该变得明亮。我们如何改变图像特定部分的亮度。请帮我解决问题,代码是

CGImageRef imageRef = imageView.image.CGImage;
CFDataRef ref = CGDataProviderCopyData(CGImageGetDataProvider(imageRef)); 
UInt8 * buf = (UInt8 *) CFDataGetBytePtr(ref); 
int length = CFDataGetLength(ref);

NSLog(@"%i",val);
float value = val/100;
for(int i=0; i<length; i+=3)
{
    Byte tempR = buf[i + 1];
    Byte tempG = buf[i + 2];
    Byte tempB = buf[i + 3];

    int outputRed = value + tempR;
    int outputGreen = value + tempG;
    int outputBlue = value + tempB;

    if (outputRed>255) outputRed=255;
    if (outputGreen>255) outputGreen=255;
    if (outputBlue>255) outputBlue=255;

    if (outputRed<0) outputRed=0;
    if (outputGreen<0) outputGreen=0;
    if (outputBlue<0) outputBlue=0;


    buf[i + 1] = outputRed; 
    buf[i + 2] = outputGreen; 
    buf[i + 3] = outputBlue;


}

size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef);
size_t bytesPerRow = CGImageGetBytesPerRow(imageRef);
CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);


CGContextRef ctx = CGBitmapContextCreate(buf, width, height, bitsPerComponent, bytesPerRow, colorSpace, CGImageGetAlphaInfo(imageRef));

CGImageRef img = CGBitmapContextCreateImage(ctx);
if (value == 0) {
    imageView.image = image;
}
else
{
    [imageView setImage:[UIImage imageWithCGImage:img]];
}

CFRelease(ref);
CGContextRelease(ctx);
CGImageRelease(img);    

1 个答案:

答案 0 :(得分:0)

经过大量的搜索,我得到了答案

//origional image
 image = [UIImage imageNamed:@"images2.jpg"];
 imageView = [[UIImageView alloc] initWithImage:image];

 CGSize size = [image size];

 [imageView setFrame:CGRectMake(0, 0, size.width, size.height)];
 [[self view] addSubview:imageView];
 [imageView release];   

 //image to be brightnessed
 CGRect rect = CGRectMake([image size].width / 6, [image size].height / 6 , 
                         ([image size].width / 2), ([image size].height / 2));

 CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
 UIImage *img = [UIImage imageWithCGImage:imageRef]; 

 imageView = [[UIImageView alloc] initWithImage:img];
 [imageView setFrame:CGRectMake([image size].width / 6, [image size].height / 6, ([image  size].width / 2), ([image size].height / 2))];
 [self.view addSubview:imageView];
 [imageView release];