1

我在 iOS 应用程序中遇到 EXC_BAD_ACCESS 错误。在模拟器和 iPhone 4S 设备上一切正常,但在 iPad2 设备上出现此错误。我在 iPhone 和 iPad2 设备上都安装了 iOS 6.0。

这是功能:我在背景图像(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;
}
4

1 回答 1

0

我怀疑您没有得到xx,yy您期望的结果,因此您超出了分配的块。尝试 NSLog 或调试xx,yy并查看byteindex最终结果。

或者如果您超出范围,请插入断言以捕获。

NSAssert( bytesPerRow * width  * yy + xx * bytesPerPixel 
  < height * width * 4, 
  @"outside range!" );

我还建议您缩短函数并将 tmp 变量移动到使用它们的块中。它使代码更易于阅读。

于 2012-11-21T17:21:31.763 回答