我有两天遇到以下问题,我无法解决,我在这里得到以下代码
+ (NSArray *)getRGB:(UIImage*)image atX:(int)xx andY:(int)yy 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 = 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;
UIColor *acolor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
[result addObject:acolor];
}
free(rawData);
return result;
}
-(void)getTouchColor:(UITapGestureRecognizer *) touch
{
UIAlertView * alert =[[[UIAlertView alloc] initWithTitle:@"entrei" message:@"tap" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil]autorelease];
[alert show];
//NSArray *Mycolors=[[[NSArray alloc] init]retain];
CGPoint point = [touch locationInView:MyImg];
//MyColors=[[NSArray alloc] init];
//GetPhoto is the name of class
NSArray *myColors=[GetPhoto getRGB:MyImg.image AtX:point.x AndY:point.y count:3];
//NSLog(@"cliquei");
}
我正在尝试填充名为MyColors getRGBAsFromImage的NSArray,但是我得到警告NSArray可能没有响应
我正在使用以下电话
我想知道我哪里错了!
非常感谢
// *对不起我的英语,向Larry Page抱怨* //
答案 0 :(得分:1)
您似乎在NSArray的实例上调用该方法(我假设实际的调用代码看起来不同?)。由于NSArray无法识别选择器,因此会发出警告。这是一个方法类?为了这个答案,我们称之为BrunosClass。然后呼叫应该是:
//remove the other line you showed.
NSArray *myColors = [BrunosClass /* <--subst with real name of class */
getRGBAsFromImage: myImg.image atX: point.x andY: point.y count: 3];
答案 1 :(得分:0)
虽然我知道解决方案应该涉及解决现有问题,但我创建了一个名为ANImageBitmapRep
的类。它允许轻松访问图像的像素数据。它在GitHub上here。使用ANImageBitmapRep,可以很容易地从像这样的图像中获取像素:
// replace myImage with your UIImage
ANImageBitmapRep * ibr = [[ANImageBitmapRep alloc] initWithImage:myImage];
BMPixel pixel = [ibr getPixelAtPoint:BMPointMake(0, 0)];
NSLog(@"Red: %f green: %f blue: %f alpha: %f", pixel.red, pixel.green, pixel.blue, pixel.alpha);
[ibr release];
您拥有的代码似乎对于在图像上获取多个像素效率非常低,因为每次需要像素时它都会重新分配整个上下文。