2011-11-14 37 views
6

Tôi đang làm một ứng dụng vẽ IPhone. Tôi muốn vẽ một phần cụ thể của hình ảnh bằng cách sử dụng touchevent để tìm dữ liệu pixel và sau đó sử dụng dữ liệu pixel đó để vẽ phần còn lại của hình ảnh. Sử dụng touchevent, tôi có giá trị pixel cho phần:Điền một phần của hình ảnh với màu

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 
    startPoint = [[touches anyObject] locationInView:imageView]; 

    NSLog(@"the value of the index is %i",index); 

    NSString* imageName=[NSString stringWithFormat:@"roof%i", index]; 

    tempColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:imageName]]; 
    lastPoint = [touch locationInView:self.view]; 
    lastPoint.y -= 20; 
    NSString *tx = [[NSString alloc]initWithFormat:@"%.0f", lastPoint.x]; 
    NSString *ty = [[NSString alloc]initWithFormat:@"%.0f", lastPoint.y]; 

    NSLog(@"the vale of the string is %@ and %@",tx,ty); 

    int ix=[tx intValue]; 
    int iy=[ty intValue]; 
    int z=1; 

    NSLog(@"the vale of the string is %i and %i and z is %i",ix,iy,z); 

    [self getRGBAsFromImage:newImage atX:ix andY:iy count1:1]; 
} 

Ở đây tôi nhận được dữ liệu pixel cho hình ảnh:

-(void)getRGBAsFromImage:(UIImage*)image atX:(int)xx andY:(int)yy count1:(int)count 
{ 
    NSMutableArray *result = [NSMutableArray arrayWithCapacity:count]; 

    // First get the image into your data buffer 
    CGImageRef imageRef = [image CGImage]; 
    NSUInteger width = CGImageGetWidth(imageRef); 
    NSUInteger height = CGImageGetHeight(imageRef); 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    unsigned char *rawData = malloc(height * width * 4); 
    NSUInteger bytesPerPixel = 4; 
    NSUInteger bytesPerRow = bytesPerPixel * width; 
    NSUInteger bitsPerComponent = 8; 
    CGContextRef context = CGBitmapContextCreate(rawData, width, height, 
               bitsPerComponent, bytesPerRow, colorSpace, 
               kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
    CGColorSpaceRelease(colorSpace); 

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); 
    CGContextRelease(context); 

    // Now your rawData contains the image data in the RGBA8888 pixel format. 
    int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel; 
    for (int ii = 0 ; ii < count ; ++ii) 
    { 
     CGFloat red = (rawData[byteIndex]  * 1.0)/255.0; 
     CGFloat green = (rawData[byteIndex + 1] * 1.0)/255.0; 
     CGFloat blue = (rawData[byteIndex + 2] * 1.0)/255.0; 
     CGFloat alpha = (rawData[byteIndex + 3] * 1.0)/255.0; 
     byteIndex += 4; 
     NSLog(@"the vale of the rbg of red is %f",red); 

     UIColor *acolor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha]; 
     [result addObject:acolor]; 
    } 

    free(rawData); 
    return result; 
} 

sử dụng một giá trị khoan dung Tôi nhận được dữ liệu. Ở đây tôi đang đấu tranh để vẽ phần còn lại.

- (BOOL)cgHitTestForArea:(CGRect)area { 
    BOOL hit = FALSE; 

    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); 

    float areaFloat = ((area.size.width * 4) * area.size.height); 
    unsigned char *bitmapData = malloc(areaFloat);  

    CGContextRef context = CGBitmapContextCreate(bitmapData, 
               area.size.width, 
               area.size.height, 
               8, 
               4*area.size.width, 
               colorspace, 
               kCGImageAlphaPremultipliedLast); 
    CGContextTranslateCTM(context, -area.origin.x, -area.origin.y); 
    [self.layer renderInContext:context]; 

    //Seek through all pixels.  
    float transparentPixels = 0; 
    for (int i = 0; i < (int)areaFloat ; i += 4) { 
     //Count each transparent pixel. 
     if (((bitmapData[i + 3] * 1.0)/255.0) == 0) { 
      transparentPixels += 1; 
     } 
    } 
    free(bitmapData); 

    //Calculate the percentage of transparent pixels. 
    float hitTolerance = [[self.layer valueForKey:@"hitTolerance"]floatValue]; 

    NSLog(@"Apixels: %f hitPercent: %f",transparentPixels,(transparentPixels/areaFloat)); 

    if ((transparentPixels/(areaFloat/4)) < hitTolerance) { 
     hit = TRUE; 
    }  

    CGColorSpaceRelease(colorspace); 
    CGContextRelease(context); 

    return hit;  
} 

Bất kỳ đề xuất nào để thực hiện công việc này?

Trả lời

1

Trước tiên, hãy chuyển hình ảnh bitmap thành một đối tượng NSArray của UIColor là các loại hạt. Cách, chi phí quá nhiều. Thay vào đó, hãy làm việc với pixelbuffer. Tìm hiểu cách sử dụng con trỏ.

http://en.wikipedia.org/wiki/Flood_fill#The_algorithm cung cấp tổng quan về một số kỹ thuật đơn giản để thực hiện điền lũ - bằng cách sử dụng đệ quy hoặc hàng đợi.

+0

tôi cũng thấy rằng tôi tryed nhiều bước bất kỳ ví dụ ứng dụng là có, chỉ nói, – Marios

+1

Đây chỉ là một điền lũ lụt, cần ví dụ một wikipedia i của nó; đã nhìn thấy điều này trước khi – Marios

+0

@arunios Bạn đã giải quyết vấn đề này? –

Các vấn đề liên quan