更改UIImage的像素颜色值

时间:2012-05-25 12:28:40

标签: iphone ios core-animation core-graphics

我想更改UIImage中所有像素的颜色。此代码成功地改变了单个点的颜色,但是当我在一个循环中运行它以获得整个图像时,我的图像就会消失。首先,我读取像素颜色值,然后将更改应用于此像素的颜色。 此外,循环仅运行i的0值。 以下是代码

    UIImage *grayImage=[UIImage imageWithCGImage:source.CGImage];
            for(int i=0;i<imageToTest.frame.size.width;i++){
                for(int j=0;j<imageToTest.frame.size.height;j++){
                    NSLog(@"%d",i);
                    CGPoint point=CGPointMake((CGFloat) i,(CGFloat) j);
                    if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, source.size.width, source.size.height), point)) {
                        return nil;
                    }

                    NSInteger pointX = trunc(point.x);
                    NSInteger pointY = trunc(point.y);
                    CGImageRef cgImage = source.CGImage;
                    NSUInteger width = CGImageGetWidth(cgImage);
                    NSUInteger height = CGImageGetHeight(cgImage);
                    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
                    int bytesPerPixel = 4;
                    int bytesPerRow = bytesPerPixel * 1;
                    NSUInteger bitsPerComponent = 8;
                    unsigned char pixelData[4] = { 0, 0, 0, 0 };
                    CGContextRef context = CGBitmapContextCreate(pixelData, 
                                                                 1,
                                                                 1,
                                                                 bitsPerComponent, 
                                                                 bytesPerRow, 
                                                                 colorSpace,
                                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
                    CGColorSpaceRelease(colorSpace);
                    CGContextSetBlendMode(context, kCGBlendModeCopy);

                        // Draw the pixel we are interested in onto the bitmap context
                    CGContextTranslateCTM(context, -pointX, -pointY);
                    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
                    CGContextRelease(context);

                        // Convert color values [0..255] to floats [0.0..1.0]
                    CGFloat red   = (CGFloat)pixelData[0] / 255.0f;
                    CGFloat green = (CGFloat)pixelData[1] / 255.0f;
                    CGFloat blue  = (CGFloat)pixelData[2] / 255.0f;
                    CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;
                    UIColor *currentPixelColor;
                    CGFloat Answer=sliderValue/10;
                    red=red*Answer;
                    green=green*Answer;
                    blue=blue*Answer;
                    alpha=alpha*Answer;
                    currentPixelColor=[UIColor colorWithRed:red green:green blue:blue alpha:alpha];


                    int width2 = source.size.width;
                    int height2 = source.size.height;

                    CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();    
                    CGContextRef context2 = CGBitmapContextCreate (nil,
                                                                   width2,
                                                                   height2,
                                                                   8,
                                                                   0,
                                                                   colorSpace2,
                                                                   kCGImageAlphaPremultipliedFirst);

                    CGColorSpaceRelease(colorSpace2);

                    if (context2 == NULL) {
                        return nil;
                    }

                    CGContextDrawImage(context2,
                                       CGRectMake(0, 0, width2, height2), grayImage.CGImage);

                    CGContextSetFillColorWithColor(context2, currentPixelColor.CGColor);
                    CGContextFillRect(context2, CGRectMake(point.x, point.y, 1, 1));

                    grayImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context2)];
                    CGContextRelease(context2);

                }

            }


            return grayImage;

}

2 个答案:

答案 0 :(得分:1)

我怀疑你遇到这种情况并且返回nil(因此你的图像消失了):

if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, source.size.width, source.size.height), point)) {
    return nil;
}

我认为您的迭代应该超过source.size.width和source.size.height而不是包含视图的维度。

另一方面,你可能正在迭代到一个高水平。只有在CGContextRef上创建并应用所有更改才有可能(当然也更有效)。老实说,我没有仔细研究你的算法,但是你应该尝试把所有不需要反复重复的东西都放在你的循环中。

答案 1 :(得分:0)

您应该考虑使用Core Image滤镜进行颜色调整。它们非常快,而且非常易于使用。 Erica Sadun的书“The iOS 5 Developer's Cookbook”包含一章使用CI非常清晰易用。它还包括一个解决操作系统错误的方法,可以防止您将修改后的映像从Core Image恢复为UIImage。