我在iOS应用中遇到EXC_BAD_ACCESS错误。在模拟器和模拟器上一切正常iPhone 4S设备但我在iPad2设备上收到此错误。我在iPhone和iPhone上安装了iOS 6.0。 iPad2设备。
这是功能:我在背景图像(UIImageView)上有一个圣诞树(UIImageView)。我在圣诞树上有很多饰品(UIImageViews),当用户摇动设备时它们都会掉下来。现在我们可以从地板上拖动装饰品并在树上装饰它们。我注意到它只在我离开/释放装饰品时才会崩溃,当它部分地触摸树时(我的意思是当装饰物触及树的边缘并且仍然在背景UIImageView上并且我释放/掉落装饰物)
CGFloat red = (rawData[byteIndex] * 1.0) / 255.0;
方法内的代码-(NSArray*)getRGBAsFromImage:(UIImage*)image atX:(int)xx andY:(int)yy count:(int)count
出现错误。以下是代码......
我尝试了各种方法来解决它,但没有用。如果有人能指引我正确的方向,我将非常感激。
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
//[Audio1 pause];
NSLog(@"touchesEnded:withEvent:");
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.view];
if (pageNum == 9) //ROCKEFELLER CHRISTMAS TREE
{
NSLog(@"touchesEnded:withEvent: if (pageNum == 9)");
float trans_y,drop_second;
UIImageView *temp_view;
// UITouch *touch=[touches anyObject];
CGPoint _location=currentPosition;
//for (int i=601; i<636; i++) {
for (int i=921; i<956; i++) {
if ([touch view] ==[self.view viewWithTag:i]) {
press_ornament=YES;
//invalidate the transform scale.
temp_view=(UIImageView *)[self.view viewWithTag:i];
temp_view.transform=CGAffineTransformIdentity;
//detect the position are inside tree, or outside
color_array= [self getRGBAsFromImage:tree.image atX:_location.x andY:_location.y count:1];
color = color_array[0];
//Call getRed:green:blue:alpha: and pass in pointers to floats to take the answer.
[color getRed: &red_color green: &green_color blue: &blue_color alpha: &alpha_value];
NSLog(@"red = %f. Green = %f. Blue = %f. Alpha = %f",red_color,green_color,blue_color,alpha_value);
// Using alpha, decide inside the tree inside the tree view domain
if ((alpha_value == 1) && (tree.frame.origin.x < _location.x) &&(_location.x <tree.frame.origin.x +tree.frame.size.width) && (tree.frame.origin.y < _location.y) && (_location.y< tree.frame.origin.y + tree.frame.size.height)) {
[touch view].center=CGPointMake(_location.x, _location.y);
}
//outside the tree fall the ornament
else{
if ([[UIDevice currentDevice]userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
trans_y=280-[touch view].frame.origin.y;
drop_second =trans_y/150;
temp_view=(UIImageView *)[self.view viewWithTag:i];
// Move the image
[self moveImage:temp_view duration:drop_second curve:UIViewAnimationCurveLinear x:temp_view.frame.origin.x y:280];
}
else{
trans_y=730-[touch view].frame.origin.y;
drop_second =trans_y/300;
temp_view=(UIImageView *)[self.view viewWithTag:i];
// Move the image
[self moveImage:temp_view duration:drop_second curve:UIViewAnimationCurveLinear x:temp_view.frame.origin.x y:730];
}
}
}
}
}
}
//This is the function that get point color.
-(NSArray*)getRGBAsFromImage:(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);
NSInteger width=tree.frame.size.width;
NSInteger height=tree.frame.size.height;
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.
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;
}
答案 0 :(得分:0)
我怀疑你没有得到你期望的xx,yy
,因此你超出了你分配的区块。尝试NSLog或调试xx,yy
,看看你最终得到的byteindex
。
或者如果你超出范围就加入一个断言来抓住。
NSAssert( bytesPerRow * width * yy + xx * bytesPerPixel
< height * width * 4,
@"outside range!" );
我还建议您缩短函数并将tmp变量移动到使用它们的块。它使代码更容易阅读。