gpt4 book ai didi

iphone - 更改 UIImage 的像素颜色值

转载 作者:行者123 更新时间:2023-11-29 13:35:12 25 4
gpt4 key购买 nike

我想改变 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;

}

最佳答案

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

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

我认为您的迭代应该超过 source.size.width 和 source.size.height 而不是包含 View 的尺寸。

附带说明一下,您可能会在较高级别进行迭代。只在 CGContextRef 上创建并应用所有更改可能是可能的(当然效率更高)。老实说,我还没有足够仔细地研究您的算法,但您应该尝试将不需要一遍又一遍地重复的所有内容都从循环中取出。

关于iphone - 更改 UIImage 的像素颜色值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10754448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com