2015-03-08 2 views
0

Я хочу получить все точки в UIImageVeiw, поэтому я могу изменить «некоторые» цвета точек без необходимости UITouch .. возможно ли это?Получить UIImageView CGPoints

Что я имею в виду это:

  1. получить все точки в UIImageView
  2. получите цвет каждой точки.
  3. если предыдущий цвет = определенный цвет, затем измените цвет.

я гугл это много, но я обнаружил, что все учебные пособия зависят от UITouch как этого http://www.markj.net/iphone-uiimage-pixel-color/

моей главной цель в том, как получить все точки?!

любая помощь ценится

ответ

0

я нашел решение .. надеюсь, это поможет кто-нибудь один день. Этот метод возвращает массив пикселей в некотором изображении.

-(NSArray*)getRGBAsFromImage:(UIImage*)image atX:(int)x andY:(int)y count:(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 = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char)); 
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. 
NSUInteger byteIndex = (bytesPerRow * y) + x * bytesPerPixel; 
for (int i = 0 ; i < count ; ++i) 
{ 
    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 += bytesPerPixel; 

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

free(rawData); 

return result;} 

можно назвать так:

NSArray*arrayOfPixels= [self getRGBAsFromImage:_patternFirstImage.image atX:_patternFirstImage.frame.origin.x andY:_patternFirstImage.frame.origin.y count:_patternFirstImage.frame.size.width*_patternFirstImage.frame.size.height]; 
NSLog(@"arrayOfPixels = %zd",[arrayOfPixels count]); 

вы можете перебрать все пиксели, чтобы получить свой цвет, как следующее:

for(int i=0;i<[arrayOfPixels count];i++){ 
    NSLog(@"objects = ---%@----",[arrayOfPixels objectAtIndex:i]); 
    if([self color:[arrayOfPixels objectAtIndex:i] isEqualToColor:UIColorFromRGB(0xE0E0E0) withTolerance:0.2]){ 
     NSLog(@"index = %zd",i); 
    } 
} 

Большое спасибо за ответ здесь: https://stackoverflow.com/a/1262893/2168496

Смежные вопросы