2013-06-23 27 views
0

我使用Core Graphics創建了許多圖像顏色變體。我需要總共創建約320個,但逐漸地,應用程序的內存使用量不斷增加,直至崩潰。從樂器,我看到更多的CGImage對象正在創建並保持活力。我想釋放它們是因爲我將圖像以PNG格式存儲到緩存目錄。UIGraphicsBeginImageContext創建的映像永遠保留在內存中

我已經找遍了所有其他我找不到的解決方案。任何幫助表示讚賞。謝謝。

這裏的主要部分:

+(UIImage *)tintedImageFromImage:(UIImage *)sourceImage colour:(UIColor *)color intensity:(float)intensity { 

    if (UIGraphicsBeginImageContextWithOptions != NULL) { 

     UIGraphicsBeginImageContextWithOptions(sourceImage.size, NO, 0.0); 

    } else { 

     UIGraphicsBeginImageContext(sourceImage.size); 
    } 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGRect rect = CGRectMake(0, 0, sourceImage.size.width, sourceImage.size.height); 

    // draw alpha-mask 
    CGContextSetBlendMode(context, kCGBlendModeNormal); 
    CGContextDrawImage(context, rect, sourceImage.CGImage); 

    // draw tint color, preserving alpha values of original image 
    CGContextSetBlendMode(context, kCGBlendModeSourceIn); 
    [color setFill]; 
    CGContextFillRect(context, rect); 


    //Set the original greyscale template as the overlay of the new image 
    sourceImage = [self verticallyFlipImage:sourceImage]; 
    [sourceImage drawInRect:CGRectMake(0,0, sourceImage.size.width,sourceImage.size.height) blendMode:kCGBlendModeMultiply alpha:intensity]; 

    UIImage *colouredImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    colouredImage = [self verticallyFlipImage:colouredImage]; 


    return colouredImage; 
} 

這是用來翻轉圖像:

+(UIImage *)verticallyFlipImage:(UIImage *)originalImage { 

    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:originalImage]; 

    UIGraphicsBeginImageContext(tempImageView.frame.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, tempImageView.frame.size.height); 

    CGContextConcatCTM(context, flipVertical); 

    [tempImageView.layer renderInContext:context]; 

    UIImage *flippedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return flippedImage; 
} 

回答

0

添加到verticallyFlipImagetempImageView.image = nil;爲了使圖像視圖發佈的圖像。這解決了這個問題。