2014-04-16 69 views
4

EXC_BAD_ACCESS錯誤我有一個函數:與CGImageRef

- (UIImage *) resizeImage:(UIImage *)image width:(CGFloat)resizedWidth height:(CGFloat)resizedHeight shouldCrop:(BOOL)crop 
{ 
    CGImageRef imageRef = [image CGImage]; 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef bitmap = CGBitmapContextCreate(NULL, resizedWidth, resizedHeight, 8, 4 * resizedWidth, colorSpace, (CGBitmapInfo) kCGImageAlphaPremultipliedFirst); 

    CGContextDrawImage(bitmap, CGRectMake(0, 0, resizedWidth, resizedHeight), imageRef); 
    CGImageRef ref = CGBitmapContextCreateImage(bitmap); 

    if (crop) // for SmallImage (tableviewcell) 
    { 
     CGRect cropRect = CGRectMake(0, CGImageGetHeight(ref) * 0.5 - ((float) kIvHeight * 0.5), (float) kIvWidth, (float) kIvHeight); 
     ref = CGImageCreateWithImageInRect(ref, cropRect); 
    } 

    UIImage * result = [UIImage imageWithCGImage:ref]; 

    CGContextRelease(bitmap); 
    CGImageRelease(ref); 
    CGImageRelease(imageRef); 
    return result; 
} 

CGImageRelease(imageRef);使我的應用程序與EXC_BAD_ACCESS 看來,當我刪除行工作墜毀線 - 但這樣做不會造成內存泄漏?

+0

沒有這一行不知何故取消分配原來的UIImage? (圖像參數) – Halpo

+2

只有在使用CGImageCreate,Copy或Retain時,才需要調用CGImageRelease。你不需要在你的情況下調用CGImageRelease –

回答