2010-10-11 4 views
0

Мне нужно выполнить операцию масштабирования на UIImage, а следующий - тот же код. вызывая утечку памяти. Невозможно отладить то, что здесь не так.Масштабирование изображения

UIImage* ScaleImageWithWH(UIImage* image, CGRect aRect) 
{ 
    int kMaxResolution = 1800; 

    int x; 
    int y; 

    x = (image.size.width * aRect.origin.x)/aRect.size.width; 
    y = (image.size.height * aRect.origin.y)/aRect.size.height; 

    CGImageRef imgRef = CGImageCreateWithImageInRect (image.CGImage, CGRectMake (x,y,aRect.size.width,aRect.size.height)); 
    CGFloat width = CGImageGetWidth(imgRef); 
    CGFloat height = CGImageGetHeight(imgRef); 

    CGAffineTransform transform = CGAffineTransformIdentity; 

    CGRect bounds = CGRectMake(aRect.origin.x, aRect.origin.y, width, height); 

    bounds.size.width = aRect.size.width; 
    bounds.size.height = aRect.size.height; 

    CGFloat scaleRatio = bounds.size.width/width; 
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)); 

    UIImageOrientation orient = image.imageOrientation; 

    switch(orient) 
    { 

     case UIImageOrientationUp: //default 
      transform = CGAffineTransformIdentity; 
      break; 

     default: 
      [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"]; 
    } 

    UIGraphicsBeginImageContext(bounds.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextScaleCTM(context, scaleRatio, -scaleRatio); 
    CGContextTranslateCTM(context, 0, -height); 

    CGContextConcatCTM(context, transform); 
    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef); 
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return imageCopy; 
} 

Пожалуйста, помогите нам в этом.

+1

Где утечка памяти и что такое предупреждение? – kubi

ответ

1

Вы должны освободить CGImageRef и CGContextRef, используя следующий код

CGImageRelease(imgRef); 
CGContextRelease(context); 
+0

Не нужно выделять контекст - он выпущен на UIGraphicsEndImageContext(). – Anton

Смежные вопросы