2013-07-08 3 views
0

원형 그라디언트를 렌더링하고 팬 제스처 인식기를 사용하여 손가락으로 스윕 할 수있는 방법을 만들었습니다.픽셀에서 가져온 RGB 값이 올바르지 않습니다.

현재 터치 위치에서 픽셀을 가져오고 색을 검색하려고합니다.

이것은 그라디언트 위로 이동하면서 색상 값이 지속적으로 업데이트되어야 함을 의미합니다. 의도 한대로

- (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]; 

} 
+0

어떤 색상을 기대하고 있으며 실제로 어떤 색상을 얻고 있습니까? – borrrden

+0

NSLog를 만들어 여기에 1 초 붙여 넣으십시오. – Exothug

+0

그림을 슬프게 너무 낮게 추가 할 수 없습니다. 그라데이션 렌더링을위한 코드를 추가했을 수도 있습니다. – Exothug

답변

0

여기에 첫 번째 문제는 당신이 colorPoint을 계산하고있는 방법입니다. 그들은 지금, colorPoint과 같은 방식으로 항상보기의 중심점이됩니다.

- (IBAction)handlePan:(UIPanGestureRecognizer *)sender 
{ 
    if (sender.numberOfTouches) 
    { 
     CGPoint lastPoint = [sender locationOfTouch: sender.numberOfTouches - 1 inView: sender.view]; 
     NSLog(@"lastPoint: %@", NSStringFromCGPoint(lastPoint)); 
    } 
} 

이 거기에서, 나는 아마 그 대신에 다시 읽는 중에 다음 비트 맵 컨텍스트에 이미지를 블리트 그리고 그 추천 할 것 :이 handlePan: 방법은 당신에게보기의 마지막 터치의 요점을 파악한다 요점은, 처음에 이미지를 생성하는 데 사용한 것과 동일한 수학적 프로세스를 사용하여 점의 색상을 계산한다는 것입니다. 지금하고있는 방식은 훨씬 더 많은 CPU + 메모리 집약적 인 방법이 될 것입니다.

편집 : 다음은 내가 생각해 낸 것입니다. 그것은 나를 위해 작동합니다. 엑스 코드에서 단일보기 응용 프로그램 템플릿에서 시작, 당신이 게시 된 코드를 사용하여, 나는 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 

내가 볼 것은 내가 그라데이션을 통해 내 손가락을 드래그 할 때, 내가 얻을 수 있다는 것입니다 내 손가락의 위치에 해당하는 RGB 값으로 꾸준히 메시지를 전달합니다. 오른쪽 아래의 작은보기에서 마지막 색상을 표시하는 코드도 추가했습니다. 또한 비트 맵 컨텍스트를 사용하지 않습니다. 희망이 도움이됩니다.

+0

사용자가 팬 제스처 인식기에서 작동하지 않습니다. 사용자가 그라디언트를 계속 드래그하여 색상을 드래그하려고하기 때문에 – Exothug

+0

내 방법을 완전한 구현. 나는 네가 지금 무엇을하지 못하는지 이해하는 것을 도우려고 노력하고있다. – ipmcc

+0

나는 그것을 이해했으며 또한 현재의 접근 방식이 극도로 메모리 집약적이라는 것을 이해합니다. 그러나 numberOfTouches는 내가 터치 대신 패닝을 사용하기 때문에 작동하지 않습니다. – Exothug

관련 문제