ios - 如何更改部分 UIImage 的颜色?
<p><p>我有一张灰度图像,我在该图像的某些部分应用了它的原始颜色,我已经实现了它。现在我想改变我在图像中应用原始颜色的那部分的颜色</p>
<p>我有这个:
<a href="http://i.stack.imgur.com/qfjsc.png" rel="noreferrer noopener nofollow">Original Image</a> </p>
<p>我想在这个转换:
<a href="http://i.stack.imgur.com/ynIWW.png" rel="noreferrer noopener nofollow">Result Image</a> </p>
<pre><code>CGImageRef imageRef = ;
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
NSUInteger bytesCount = height * width * bytesPerPixel;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char *)calloc(bytesCount, sizeof(unsigned char));
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
unsigned char *outputData = (unsigned char *)calloc(bytesCount, sizeof(unsigned char));
NSUInteger byteIndex = 0;
for (NSUInteger i=0; i<bytesCount / bytesPerPixel; ++i) {
CGFloat red = (CGFloat)rawData;
CGFloat green = (CGFloat)rawData;
CGFloat blue = (CGFloat)rawData;
CGFloat alpha = (CGFloat)rawData;
BOOL grayscale = red == green == blue;
if (!grayscale) {
// test for near values
CGFloat diff = MAX(ABS(red-green), MAX(ABS(red-blue), ABS(green-blue)));
static CGFloat allowedDifference = 100; // in range of 0-255
if (diff > allowedDifference) {
// CGFloat redTemp = 236;
// red = green;
// green = redTemp;
red = 236.0;
green = 17.0;
blue = 17.0;
}
}
outputData = red;
outputData = green;
outputData = blue;
outputData = alpha;
byteIndex += bytesPerPixel;
}
free(rawData);
CGDataProviderRef outputDataProvider = CGDataProviderCreateWithData(NULL,
outputData,
bytesCount,
NULL);
free(outputData);
CGImageRef outputImageRef = CGImageCreate(width,
height,
bitsPerComponent,
bytesPerPixel * 8,
bytesPerRow,
colorSpace,
kCGBitmapByteOrderDefault,
outputDataProvider,
NULL,NO,
kCGRenderingIntentDefault);
CGColorSpaceRelease(colorSpace);
CGDataProviderRelease(outputDataProvider);
UIImage *outputImage = ;
CGImageRelease(outputImageRef);
</code></pre>
<p>我尝试了 <code>bitmapcontext</code> 和一切,但没有得到想要的结果。</p>
<p>有人知道吗?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您可以尝试使用 CGBitmapContextCreate 从图像中获取像素数据来创建颜色空间,然后通过 CGContextDrawImage 将图像绘制到它。</p>
<p>其次,您将收到一个一维字节数组。
像这样: 其中 r,g,b,a - 颜色分量,1,2 - nu。像素。</p>
<p>在此之后,您可以遍历数组并比较每个像素的颜色分量。由于要跳过灰度像素,所以需要比较 rgb 参数,理论上它们必须相等,但也可以支持几位数的一些小错误 +-。</p>
<p>如果具体像素不是灰度,则只需交换红色和绿色字节。</p>
<p>应该是要走的路。</p>
<p>更新示例:</p>
<pre><code>UIImage *image = ;
CGImageRef imageRef = ;
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
NSUInteger bytesCount = height * width * bytesPerPixel;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char *)calloc(bytesCount, sizeof(unsigned char));
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
unsigned char *outputData = (unsigned char *)calloc(bytesCount, sizeof(unsigned char));
NSUInteger byteIndex = 0;
for (NSUInteger i=0; i<bytesCount / bytesPerPixel; ++i) {
CGFloat red = (CGFloat)rawData;
CGFloat green = (CGFloat)rawData;
CGFloat blue = (CGFloat)rawData;
CGFloat alpha = (CGFloat)rawData;
BOOL grayscale = red == green == blue;
if (!grayscale) {
// test for near values
CGFloat diff = MAX(ABS(red-green), MAX(ABS(red-blue), ABS(green-blue)));
static CGFloat allowedDifference = 50.0; // in range of 0-255
if (diff > allowedDifference) {
CGFloat redTemp = red;
red = green;
green = redTemp;
}
}
outputData = red;
outputData = green;
outputData = blue;
outputData = alpha;
byteIndex += bytesPerPixel;
}
free(rawData);
CGDataProviderRef outputDataProvider = CGDataProviderCreateWithData(NULL,
outputData,
bytesCount,
NULL);
free(outputData);
CGImageRef outputImageRef = CGImageCreate(width,
height,
bitsPerComponent,
bytesPerPixel * 8,
bytesPerRow,
colorSpace,
kCGBitmapByteOrderDefault,
outputDataProvider,
NULL,NO,
kCGRenderingIntentDefault);
CGColorSpaceRelease(colorSpace);
CGDataProviderRelease(outputDataProvider);
UIImage *outputImage = ;
CGImageRelease(outputImageRef);
</code></pre>
<p>注意静态允许的差异变量。它允许您跳过几乎非灰度像素,但它们在 RGB 颜色空间中并且本质上几乎是灰度的。</p>
<p>以下是示例:</p>
<ol>
<li>允许的差异 = 0
<a href="/image/yq2Xp.png" rel="noreferrer noopener nofollow"><img src="/image/yq2Xp.png" alt="Allowed difference = 0"/></a> </li>
<li>允许的差值 = 50
<a href="/image/hMMUu.png" rel="noreferrer noopener nofollow"><img src="/image/hMMUu.png" alt="Allowed difference = 50"/></a> </li>
</ol></p>
<p style="font-size: 20px;">关于ios - 如何更改部分 UIImage 的颜色?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/33521152/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/33521152/
</a>
</p>
页:
[1]