2012-09-15 39 views
2

我在圖像上實現了一個遞歸函數,它調用其相鄰像素,直到條件完成,我已經能夠成功運行此代碼,最高分辨率爲200x200,但當圖像大小增加時,將在下面的堆棧行中出現EXC_BAD_ACCESS錯誤___ lldb_unnamed_function782 $$ libicucore.A.dylib。我檢查了我的代碼,無法檢測到任何錯誤,可能是因爲遞歸回調函數太多。如果有人有任何想法,請讓我知道。在大圖像分辨率下執行時出現EXC_BAD_ACCESS錯誤

這是我的遞歸代碼:

-(void)magicImageContext:(unsigned char*)data point:(CGPoint)point red:(unsigned char)red green:(unsigned char)green blue:(unsigned char)blue bytesPerPixel:(NSInteger)bytesPerPixel bytesPerRow:(NSInteger)bytesPerRow size:(long long int)size width:(NSInteger)width height:(NSInteger)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];    } 
     } 
    } 
} 
} 
+0

如果你解決了這個問題,你可以粘貼代碼嗎? – Levi

回答

3

如果您使用整個圖像的所有像素遞歸,然後輸入一個大的圖像肯定會導致堆棧溢出。

考慮重寫你的函數使用顯式堆棧和循環來避免遞歸。這也可以提高應用程序的性能,因爲它避免了相對昂貴的函數調用。