2011-07-31 59 views
1

我的代碼有一個很奇怪的行爲。我想在UIGraphicsImageContext中編輯一些圖形化的東西。有時它起作用,有時它不起作用。我取下圖像等待處理功能,以一個新的線程,看起來像這樣:UIImage上下文編輯導致SIGABRT/EXC_BAD_ACCESS

-(void)imageColorTintChanged:(id)sender { 
[self process:@selector(tint:) withObject:sender];} 

-(void)tint:(id)sender { 
@synchronized(image) { 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    float f  = ((UISlider *)sender).value; 
    NSInteger tag = ((UISlider *)sender).tag; 
    if (tag == 0) { 
     redTint = f; 
    } else if (tag == 1) { 
     greenTint = f; 
    } else if (tag == 2) { 
     blueTint = f; 
    } 
    previewImage = [ImageUtil colorizeImage:image color:[UIColor colorWithRed:redTint green:greenTint blue:blueTint alpha:1.0]]; 
    [imageView setImage:previewImage]; 
    [self processDidFinish]; 
    [pool release]; 
}} 

最後我的圖形編輯開始:處理功能是由喜歡這個功能,一個叫

-(void)process:(SEL)function withObject:(id)sender { 
UIActivityIndicatorView *av = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease]; 
av.frame = CGRectMake(round((self.view.frame.size.width - 50)/2), 
         round((self.view.frame.size.height - 50)/2), 50, 50); 
av.tag = kActivityTag; 
[self.view addSubview:av]; 
[av startAnimating]; 
[self enableControls:NO]; 
[NSThread detachNewThreadSelector:function toTarget:self withObject:sender]; 

之前這裏的新線程:

+ (UIImage *)colorizeImage:(UIImage *)baseImage color:(UIColor *)theColor { 
if (baseImage) { 
    @synchronized (baseImage) { 
     UIGraphicsBeginImageContext(baseImage.size); // CRASH!! 

     CGContextRef ctx = UIGraphicsGetCurrentContext(); 
     CGRect area = CGRectMake(0.0f, 0.0f, baseImage.size.width, baseImage.size.height); 

     CGContextTranslateCTM(ctx, 0.0, baseImage.size.height); 
     CGContextScaleCTM(ctx, 1.0, -1.0);  

     [theColor set]; 
     CGContextFillRect(ctx, area); 

     CGContextSetBlendMode(ctx, kCGBlendModeMultiply); 

     CGContextDrawImage(ctx, area, baseImage.CGImage); 

     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 

     UIGraphicsEndImageContext(); 

     return newImage; 
    } 
} 
return baseImage; 

它的大部分在這一行崩潰的時間:

UIGraphicsBeginImageContext(baseImage.size);

它可能是什麼?穿線的東西? 在此先感謝。

編輯:

好了,我對儀器和出於某種原因它說沒有內存泄漏作出了活動監視器的測試,但真正的內存使用量上升到一定的圖像處理後的53 MB,然後崩潰。是否有可能只是一些「我使用太多內存」錯誤?

回答

1

UIKit Function Reference,關於UIGraphicsBeginImageContext()

你應該調用只從應用程序的主線程此功能。

您需要在此處創建一個CGBitmapContext

+0

修好了! :D 謝謝+1 –