Objective c NSArray可能不会响应
我有两天以下的问题,我不能解决,我得到了下面的代码就在这里Objective c NSArray可能不会响应,objective-c,Objective C,我有两天以下的问题,我不能解决,我得到了下面的代码就在这里 + (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 imag
+ (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");
}
+(NSArray*)getRGB:(UIImage*)图像atX:(int)xx安迪:(int)yy计数:(int)计数
{
NSMUTABLEARRY*result=[NSMUTABLEARRY阵列容量:计数];
//首先将图像放入数据缓冲区
CGImageRef imageRef=[图像CGImage];
NSU整数宽度=CGImageGetWidth(imageRef);
NSU整数高度=CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
无符号字符*rawData=malloc(高度*宽度*4);
NSU整数字节/像素=4;
NSUInteger bytesPerRow=bytesPerPixel*宽度;
NSU整数比特分量=8;
CGContextRef context=CGBitmapContextCreate(原始数据、宽度、高度、,
bitsPerComponent、bytesPerRow、颜色空间、,
KCGIMAGEAlphaPremultipledLast | kCGBitmapByteOrder32Big);
CGCOLORSPACTERELEASE(色彩空间);
CGContextDrawImage(上下文,CGRectMake(0,0,宽度,高度),imageRef);
CGContextRelease(上下文);
//现在,您的rawData包含RGBA8888像素格式的图像数据。
int byteIndex=(bytesPerRow*yy)+xx*bytesperpoixel;
对于(int ii=0;ii
我正在尝试填充名为MyColors getRGBAsFromImage的NSArray作为的结果,但我得到NSArray可能不会响应的警告
我正在使用以下呼叫
我不知道我错在哪里
多谢各位
//*对不起,我说的是英语,请向Larry Page投诉*/您似乎在NSArray实例上调用该方法(我假设实际调用的代码看起来有所不同?)。由于NSArray无法识别选择器,因此会发出警告。这是哪一类的方法?为了这个答案,让我们称之为布鲁诺格拉斯。那么电话应该是:
//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];
//删除显示的另一行。
NSArray*myColors=[BrunosClass/*虽然我知道解决方案应该包括修复现有问题,但我创建了一个名为ANImageBitmapRep
的类。它允许轻松访问图像的像素数据。它位于GitHub上。使用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];
您拥有的代码在获取图像上的多个像素时似乎效率很低,因为每次需要一个像素时,它都会重新分配整个上下文。1)第一个代码段位于哪个类?2)在第二个代码段中,MyColors
应该是类名还是变量?getRGBAsFromImage b在调用之前..我只是没有工作..MyColor是NSArray类型的对象感谢您的快速响应我尝试了这种方法,但使用了self,因此对getPhoto(类名)的唯一更改警告:很抱歉,但我不明白你到底在说什么。这个类的名称是getPhoto
?为什么不显示调用该方法的确切代码?不要键入它,从编辑器中复制并粘贴它,直接粘贴到你的问题中(编辑问题)。你说的“使用自我”是什么意思?你如何使用self
?鲁迪谢谢你的帮助,因为我编辑了代码,很抱歉我的英语