Ios 从像素检索到的RGB值不正确
我已经渲染了一个圆形渐变,并创建了一个方法,让我可以使用平移手势识别器,用手指扫过它 我正在检索当前触摸位置的像素,并希望检索其颜色 这意味着,在渐变上移动时,颜色值应不断更新 我正在使用以下代码:Ios 从像素检索到的RGB值不正确,ios,objective-c,rgb,Ios,Objective C,Rgb,我已经渲染了一个圆形渐变,并创建了一个方法,让我可以使用平移手势识别器,用手指扫过它 我正在检索当前触摸位置的像素,并希望检索其颜色 这意味着,在渐变上移动时,颜色值应不断更新 我正在使用以下代码: - (IBAction)handlePan:(UIPanGestureRecognizer *)sender { CGPoint translation = [sender translationInView:iv]; [sender setTranslation:CGPoint
- (IBAction)handlePan:(UIPanGestureRecognizer *)sender {
CGPoint translation = [sender translationInView:iv];
[sender setTranslation:CGPointZero inView:self.view];
CGPoint center = sender.view.center;
center.x += translation.x;
center.y += translation.y;
sender.view.center = center;
CGPoint colorPoint = [sender.view.superview convertPoint:center toView:iv];
[sender setTranslation:CGPointMake(0, 0) inView:self.view];
CGImageRef image = img.CGImage;
NSUInteger width = CGImageGetWidth(image);
NSUInteger height = CGImageGetHeight(image);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
char *rawData = malloc(height * width * 4);
int bytesPerPixel = 4;
int bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(
rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big
);
CGContextSetBlendMode(context, kCGBlendModeCopy);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
CGContextRelease(context);
int byteIndex = (bytesPerRow * colorPoint.y) + colorPoint.x * bytesPerPixel;
unsigned char red = rawData[byteIndex];
unsigned char green = rawData[byteIndex+1];
unsigned char blue = rawData[byteIndex+2];
UIColor *hauptfarbe = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
ch.backgroundColor = hauptfarbe;
NSLog(@"Color value - R : %i G : %i : B %i",red, green, blue);
}
这不符合预期,给了我错误的颜色,并且根本没有显示一些颜色(如红色)
编辑:我还不能添加图片,因为代表性低。我现在将添加渲染渐变的代码
代码:
- (void)viewDidLoad
{
[super viewDidLoad];
CGSize size = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width, size.height), YES, 0.0);
[[UIColor whiteColor] setFill];
UIRectFill(CGRectMake(0, 0, size.width, size.height));
int sectors = 180;
float radius = MIN(size.width, size.height)/2;
float angle = 2 * M_PI/sectors;
UIBezierPath *bezierPath;
for ( int i = 0; i < sectors; i++)
{
CGPoint center = CGPointMake((size.width/2), (size.height/2));
bezierPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:i * angle endAngle:(i + 1) * angle clockwise:YES];
[bezierPath addLineToPoint:center];
[bezierPath closePath];
UIColor *color = [UIColor colorWithHue:((float)i)/sectors saturation:1. brightness:1. alpha:1];
[color setFill];
[color setStroke];
[bezierPath fill];
[bezierPath stroke];
}
img = UIGraphicsGetImageFromCurrentImageContext();
iv = [[UIImageView alloc] initWithImage:img];
[self.view addSubview:iv];
[self.view addSubview:ch];
}
-(void)viewDidLoad
{
[超级视图下载];
CGSize size=CGSizeMake(self.view.bounds.size.width,self.view.bounds.size.height);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width,size.height),YES,0.0);
[[UIColor whiteColor]setFill];
UIRectFill(CGRectMake(0,0,size.width,size.height));
int扇区=180;
浮动半径=最小值(尺寸.宽度,尺寸.高度)/2;
浮动角度=2*M_PI/扇区;
UIBezierPath*bezierPath;
对于(int i=0;i这里的第一个问题是计算色点的方法。按照现在的方式,colorPoint
将始终是视图的中心点。此handlePan:
方法应能让您获得视图中最后一次触摸的点:
- (IBAction)handlePan:(UIPanGestureRecognizer *)sender
{
if (sender.numberOfTouches)
{
CGPoint lastPoint = [sender locationOfTouch: sender.numberOfTouches - 1 inView: sender.view];
NSLog(@"lastPoint: %@", NSStringFromCGPoint(lastPoint));
}
}
从这里开始,我可能会建议您不要将图像分插到位图上下文中,然后在该点尝试从中读取,而只需使用与创建图像时相同的数学过程来计算该点的颜色。您现在的做法将更加CPU+内存密集型
编辑:以下是我的想法。它对我有用。从Xcode中的单视图应用程序模板开始,使用您发布的代码,我在ViewController.m中有以下代码:
@implementation ViewController
{
UIImage* img;
UIImageView* iv;
}
- (void)viewDidLoad
{
[super viewDidLoad];
CGSize size = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width, size.height), YES, 0.0);
[[UIColor whiteColor] setFill];
UIRectFill(CGRectMake(0, 0, size.width, size.height));
int sectors = 180;
float radius = MIN(size.width, size.height)/2;
float angle = 2 * M_PI/sectors;
UIBezierPath *bezierPath;
for ( int i = 0; i < sectors; i++)
{
CGPoint center = CGPointMake((size.width/2), (size.height/2));
bezierPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:i * angle endAngle:(i + 1) * angle clockwise:YES];
[bezierPath addLineToPoint:center];
[bezierPath closePath];
UIColor *color = [UIColor colorWithHue:((float)i)/sectors saturation:1. brightness:1. alpha:1];
[color setFill];
[color setStroke];
[bezierPath fill];
[bezierPath stroke];
}
img = UIGraphicsGetImageFromCurrentImageContext();
iv = [[UIImageView alloc] initWithImage:img];
[self.view addSubview:iv];
colorView = [[UIView alloc] init];
colorView.frame = CGRectMake(CGRectGetMaxX(bounds) - 25, CGRectGetMaxY(bounds) - 25, 20, 20);
[self.view addSubview:colorView];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:@selector(handlePan:)];
[self.view addGestureRecognizer: panGesture];
}
- (IBAction)handlePan:(UIPanGestureRecognizer *)sender
{
if (sender.numberOfTouches)
{
CGPoint lastPoint = [sender locationOfTouch: sender.numberOfTouches - 1 inView: sender.view];
CGRect bounds = self.view.bounds;
CGPoint center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
CGPoint delta = CGPointMake(lastPoint.x - center.x, lastPoint.y - center.y);
CGFloat angle = (delta.y == 0 ? delta.x >= 0 ? 0 : M_PI : atan2(delta.y, delta.x));
angle = fmod(angle, M_PI * 2.0);
angle += angle >= 0 ? 0 : M_PI * 2.0;
UIColor *color = [UIColor colorWithHue: angle / (M_PI * 2.0) saturation:1. brightness:1. alpha:1];
colorView.backgroundColor = color;
CGFloat r,g,b,a;
if ([color getRed: &r green: &g blue:&b alpha: &a])
{
NSLog(@"Color value - R : %g G : %g : B %g", r, g, b);
}
}
}
@end
@实现视图控制器
{
UIImage*img;
UIVIEW*iv;
}
-(无效)viewDidLoad
{
[超级视图下载];
CGSize size=CGSizeMake(self.view.bounds.size.width,self.view.bounds.size.height);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width,size.height),YES,0.0);
[[UIColor whiteColor]setFill];
UIRectFill(CGRectMake(0,0,size.width,size.height));
int扇区=180;
浮动半径=最小值(尺寸.宽度,尺寸.高度)/2;
浮动角度=2*M_PI/扇区;
UIBezierPath*bezierPath;
对于(int i=0;i=0?0:M_PI:atan2(δy,δx));
角度=fmod(角度,M_PI*2.0);
角度+=角度>=0?0:M_PI*2.0;
UIColor*color=[uicolorWith色调:角度/(μPI*2.0)饱和度:1.亮度:1.α:1];
colorView.backgroundColor=颜色;
cgr,g,b,a;
if([color getRed:&r green:&g blue:&b alpha:&a])
{
NSLog(@“颜色值-R:%g:%g:B%g”,R,g,B);
}
}
}
@结束
我看到的是,当我在梯度上拖动手指时,我会得到一个稳定的消息流,其中包含与我手指位置相对应的RGB值。我还添加了在右下角的小视图中显示最后一种颜色的代码。它也不使用位图上下文。希望这有帮助。你想要什么颜色,你实际得到什么颜色?我将制作一个NSLog并粘贴到这里,一秒钟内不能添加图片,遗憾的是重复率太低。但是添加了渐变渲染的代码,这可能会解决第一个问题,这就是你计算色点的方法。它始终是视图的中心点,所以无论发生什么,你都会得到相同的颜色。你能帮我吗?你的朋友