English 中文(简体)
更改 UIImaage 的像素色彩值
原标题:Change Pixel colour values of UIImage

I want to change the color of all pixels in UIImage. This code successfuly change the colour for a single point but when I run it in a loop for whole image then my Image disappears. First I read the pixel colour value then I apply changes to the colour of this pixel. Also loop runs for i s 0 value only. Below is the code

    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;

}
最佳回答

我怀疑你正跑进这个状况 返回零(因此你的形象消失了):

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

我认为你的迭代应该超过源的. size. width和源的. size. leight, 而不是包含视图的维度。

顺便提一句,你可能正在升级到一个高度。也许(而且肯定更有效率的)只可以在 CGContextRef 上创建并应用你的所有修改。 老实说,我还没有足够近地看待你的算法,但你应该尝试把所有不需要反复重复的东西都拿走。

问题回答

您应该查看核心图像过滤器来调整您的颜色。 它们非常快速, 并且非常容易使用。 Erica Sadun 的书“ iOS 5 Developers s Cookbook ” 包含了一个关于使用 CI 的章节, 它非常清楚, 很容易使用。 它还包括一个操作系统错误的变通办法, 使您无法将修改过的图像从核心图像恢复到 UIIMage 。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签