OStack程序员社区-中国程序员成长平台

标题: iphone - 在大图像分辨率上执行时出现 EXC_BAD_ACCESS 错误 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 16:19
标题: iphone - 在大图像分辨率上执行时出现 EXC_BAD_ACCESS 错误

我已经在图像上实现了一个递归函数,它调用它的相邻像素直到条件最终确定,我已经能够成功运行此代码,达到 200x200 图像分辨率,但是当图像大小增加时,它会因 EXC_BAD_ACCESS< 而崩溃 错误在以下堆栈行 ___lldb_unnamed_function782$$libicucore.A.dylib。我检查了我的代码并且无法检测到任何错误,可能是由于递归回调函数太多。如果有人有任何想法,请告诉我。

这是我的递归代码:

 -(void)magicImageContextunsigned char*)data pointCGPoint)point redunsigned char)red greenunsigned char)green blueunsigned char)blue bytesPerPixelNSInteger)bytesPerPixel bytesPerRowNSInteger)bytesPerRow sizelong long int)size widthNSInteger)width heightNSInteger)height maskedData:(unsigned char*)masked_data{
for(int x = point.x-1; x<=point.x+1; x++){
    for(int y = point.y-1; y<=point.y+1; y++){
        if((x == point.x) && (y == point.y)) {
        }
        else if((x<0) || (y<0) || (x>=width) || (y>=height)){
        }
        else if([self checkPixelMarkedAtPoint:CGPointMake(x, y) data:masked_data]){
            int byteIndex = (bytesPerRow * y) + x* bytesPerPixel;
            CGFloat red2   = (data[byteIndex] );
            CGFloat green2 = (data[byteIndex + 1]);
            CGFloat blue2  = (data[byteIndex + 2]);
            if([self checkColorThresholdWithRed1:red green1:green blue1:blue red2:red2 green2:green2 blue2:blue2]){
                NSLog(@"x= %d, y= %d %d",x,y,byteIndex);
                //mark pixels on masked image
                [self changemaskedData:CGPointMake(x,y) data:masked_data];
                [self magicImageContext:data point:CGPointMake(x, y) red:red green:green blue:blue bytesPerPixel:bytesPerPixel bytesPerRow:bytesPerRow size:size width:width height:height maskedData:masked_data];                }
        }
    }
}
}



Best Answer-推荐答案


如果您对图像的所有像素使用递归,那么输入大图像肯定会导致堆栈溢出。

考虑重写您的函数以使用显式堆栈和循环以避免递归。这也可以提高应用的性能,因为它避免了相对昂贵的函数调用。

关于iphone - 在大图像分辨率上执行时出现 EXC_BAD_ACCESS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12435404/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4