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;
}