<分区>
<分区>
我想用另一种颜色改变 UIImage 的颜色,首先我想触摸图像并检测触摸像素的颜色,然后用另一种颜色替换触摸像素的颜色。
我有以下代码,我试图改变触摸像素的颜色,但它返回透明图像。
- (UIColor *)colorAtPixel:(CGPoint)point
{
// Cancel if point is outside image coordinates
if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.size.width, self.size.height), point)) {
return nil;
}
// Create a 1x1 pixel byte array and bitmap context to draw the pixel into.
// Reference: http://stackoverflow.com/questions/1042830/retrieving-a-pixel-alpha-value-for-a-uiimage
NSInteger pointX = trunc(point.x);
NSInteger pointY = trunc(point.y);
CGImageRef cgImage = self.CGImage;
NSUInteger width = self.size.width;
NSUInteger height = self.size.height;
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-(CGFloat)height);
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]
red = (CGFloat)pixelData[0] / 255.0f;
green = (CGFloat)pixelData[1] / 255.0f;
blue = (CGFloat)pixelData[2] / 255.0f;
alpha = (CGFloat)pixelData[3] / 255.0f;
[self changeWhiteColorTransparent:imageview.img];
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
-(void)changeWhiteColorTransparent: (UIImage *)image{
CGImageRef rawImageRef = image.CGImage;
const float colorMasking[6] = { red, 0, green, 0, blue, 0 };
UIGraphicsBeginImageContext(image.size);
CGImageRef maskedImageRef = CGImageCreateWithMaskingColors(rawImageRef, colorMasking);
{
//if in iphone
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, image.size.height);
CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
}
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height), maskedImageRef);
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
CGImageRelease(maskedImageRef);
UIGraphicsEndImageContext();
NSString *imagespath =[self createDirectoryInDocumentsFolderWithName:@"images"];
NSFileManager *fileM = [NSFileManager defaultManager];
NSArray *contents= [fileM contentsOfDirectoryAtPath:imagespath error:nil];
NSString *savedImagePath = [imagespath stringByAppendingPathComponent:[NSString stringWithFormat:@"images%d.png",[contents count] + 1]];
NSData *imageData = UIImagePNGRepresentation(result);
[imageData writeToFile:savedImagePath atomically:NO];
}
我在这里尝试使触摸的颜色透明,但如果我想将其更改为黄色,那么我需要更改我的代码吗?
最佳答案
从 uitouch 移动调用此方法...
-(void) getPixelColorAtLocation:(CGPoint)point
{
unsigned char pixel[4] = {0};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(context, -point.x, -point.y);
[self.layer renderInContext:context];
// NSLog(@"x- %f y- %f",point.x,point.y);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
NSLog(@"RGB Color code :%d %d %d",pixel[0],pixel[1],pixel[2]);
}
set this RGB value to your imageview .
您可以在 RGB colr 组合中获取触摸点的颜色代码。试试吧。
它会帮助你。
关于ios - 如何用其他颜色替换 UIImage 中的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19360768/
相关文章:
ios - 如何在带有 Storyboard 的 UITabBarController 中实现 UISplitview?
iphone - 将 .xib 文件添加到 Storyboard 中的 ViewController
iphone - 将 UIImage 转换为 PDF 时应用程序崩溃
ios - LLVM 代码覆盖率/Slather 中实现了哪些覆盖率标准?
ios - AppDelegate (iOS) 中的“使用未声明的标识符”
ios - 有什么方法可以在 appdelegate 中为项目中的所有 UIImage 属性使用 `UIImageRenderingModeAlwaysOriginal`