[英]EXC_BAD_ACCESS while Flood Filling in Objective C
我在iPad應用程序中當前使用的目標c中編寫了一個遞歸泛洪填充方法。
我通過圖像的RGBA原始數據獲得用戶在UIImage中觸摸的點的顏色。
問題是它運行了一段時間,然后在訪問原始數據時應用程序崩潰並顯示EXC_BAD_ACCESS。 我的問題是為什么這是“堆棧溢出”? 並且任何人都可以提出解決此問題的方法/對此方法進行改進。
這是我的方法(很抱歉,這可能會更干凈)。
-(unsigned char *)floodFill:(unsigned char *)data withImageRef:(CGImageRef)imgRef withColor:(UIColor *)color whereColor:(UIColor *)pixelColor atX:(int)xx andY:(int)yy
{
//create points for top bottom left and right pixel
CGPoint top = CGPointMake(xx, yy-1);
CGPoint bottom = CGPointMake(xx, yy+1);
CGPoint left = CGPointMake(xx-1, yy);
CGPoint right = CGPointMake(xx+1, yy);
//convert new color to rgba values
const CGFloat *rgb = CGColorGetComponents(color.CGColor);
float newRed = rgb[0];
float newGreen = rgb[1];
float newBlue = rgb[2];
float newAlpha = CGColorGetAlpha(color.CGColor);
//convert old color to rgba values
const CGFloat *rgb2 = CGColorGetComponents(pixelColor.CGColor);
float oldRed = rgb2[0];
float oldGreen = rgb2[1];
float oldBlue = rgb2[2];
float oldAlpha = CGColorGetAlpha(pixelColor.CGColor);
NSUInteger width = CGImageGetWidth(imgRef);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel;
//color current pixel
data[byteIndex] = (char)newRed*255;
data[byteIndex+1] = (char)newGreen*255;
data[byteIndex+2] = (char)newBlue*255;
data[byteIndex+3] = (char)newAlpha*255;
CGFloat red, green, blue, alpha;
CGPoint currentPoint;
//check top pixel
currentPoint=top;
if(currentPoint.x>=0 && currentPoint.y>=0)
{
byteIndex = (bytesPerRow * currentPoint.y) + currentPoint.x * bytesPerPixel;
red = (data[byteIndex] * 1.0) / 255.0;
green = (data[byteIndex + 1] * 1.0) / 255.0;
blue = (data[byteIndex + 2] * 1.0) / 255.0;
alpha = (data[byteIndex + 3] * 1.0) / 255.0;
if(red==oldRed&&green==oldGreen&&blue==oldBlue&&alpha==oldAlpha)
data=[self floodFill:data withImageRef:imgRef withColor:color whereColor:pixelColor atX:currentPoint.x andY:currentPoint.y];
}
//check bottom pixel
currentPoint=bottom;
if(currentPoint.x>=0 && currentPoint.y>=0)
{
byteIndex = (bytesPerRow * currentPoint.y) + currentPoint.x * bytesPerPixel;
red = (data[byteIndex] * 1.0) / 255.0;
green = (data[byteIndex + 1] * 1.0) / 255.0;
blue = (data[byteIndex + 2] * 1.0) / 255.0;
alpha = (data[byteIndex + 3] * 1.0) / 255.0;
if(red==oldRed&&green==oldGreen&&blue==oldBlue&&alpha==oldAlpha)
data=[self floodFill:data withImageRef:imgRef withColor:color whereColor:pixelColor atX:currentPoint.x andY:currentPoint.y];
}
//check left pixel
currentPoint=left;
if(currentPoint.x>=0 && currentPoint.y>=0)
{
byteIndex = (bytesPerRow * currentPoint.y) + currentPoint.x * bytesPerPixel;
red = (data[byteIndex] * 1.0) / 255.0;
green = (data[byteIndex + 1] * 1.0) / 255.0;
blue = (data[byteIndex + 2] * 1.0) / 255.0;
alpha = (data[byteIndex + 3] * 1.0) / 255.0;
if(red==oldRed&&green==oldGreen&&blue==oldBlue&&alpha==oldAlpha)
data=[self floodFill:data withImageRef:imgRef withColor:color whereColor:pixelColor atX:currentPoint.x andY:currentPoint.y];
}
//check right pixel
currentPoint=right;
if(currentPoint.x>=0 && currentPoint.y>=0)
{
byteIndex = (bytesPerRow * currentPoint.y) + currentPoint.x * bytesPerPixel;
red = (data[byteIndex] * 1.0) / 255.0;
green = (data[byteIndex + 1] * 1.0) / 255.0;
blue = (data[byteIndex + 2] * 1.0) / 255.0;
alpha = (data[byteIndex + 3] * 1.0) / 255.0;
if(red==oldRed&&green==oldGreen&&blue==oldBlue&&alpha==oldAlpha)
data=[self floodFill:data withImageRef:imgRef withColor:color whereColor:pixelColor atX:currentPoint.x andY:currentPoint.y];
}
return data;
}
謝謝-TJ
遞歸方法是導致堆棧溢出異常的主要原因,因為每個遞歸調用都被推入堆棧直到達到結束條件,尤其是在洪水填充算法中,這種方法根本不適用,與現代計算機相比,ipad的資源有限,請考慮使用基於循環的方法填充算法或掃描線算法的實現
有關更多信息,請訪問此鏈接洪水填充算法
您必須檢查是否沒有超出為圖像分配的內存。 您正在檢查currentPoint.x>=0 && currentPoint.y>=0
。 但這並不能阻止您在最后一行上通過圖像的末尾。
您需要檢查currentPoint.y < height && currentPoint.x < width
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.