2014-02-21 72 views
0

我使用下面的代碼來創建圖像周圍的邊框。我通過多次循環創建具有邊界的縮略圖圖像,然後將這些圖像放置在UICollectionView中。重新加載UICollectionView與UIImages不釋放內存

問題似乎是,每次我重新加載UICollectionView時,圖像都不會從內存中釋放出來,而且它似乎會累積到崩潰的地步。我不認爲這是UICollectionView代碼,因爲如果我使用不需要邊框的圖像運行它,我不會遇到任何問題。

- (UIImage *)applyFrame:(UIImage *)image selectedFrame:(NSString *)selectedFrame { 

UIImage *frame; 

NSLog(@"Image Size For Frames: %f, %f",image.size.width,image.size.height); 

if(image.size.width == image.size.height){ 

    frame = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@-Square.png",selectedFrame] ofType:nil]]; 


} 

if(image.size.width > image.size.height){ 

    frame = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@.png",selectedFrame] ofType:nil]]; 


} 

if(image.size.width < image.size.height){ 


    frame = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@.png",selectedFrame] ofType:nil]]; 

    frame = [self rotateUIImage:frame clockwise:true]; 

} 

GPUImageAlphaBlendFilter *blendFilter = [[GPUImageAlphaBlendFilter alloc] init]; 
GPUImagePicture *imageToProcess = [[GPUImagePicture alloc] initWithImage:image]; 
GPUImagePicture *border = [[GPUImagePicture alloc] initWithImage:frame]; 

blendFilter.mix = 1.0f; 
[imageToProcess addTarget:blendFilter]; 
[border processImage]; 
[border addTarget:blendFilter]; 

[imageToProcess processImage]; 

return [blendFilter imageFromCurrentlyProcessedOutput]; 
} 

- (UIImage*)rotateUIImage:(UIImage*)sourceImage clockwise:(BOOL)clockwise { 
CGSize size = sourceImage.size; 
UIGraphicsBeginImageContext(CGSizeMake(size.height, size.width)); 
[[UIImage imageWithCGImage:[sourceImage CGImage] scale:1.0 orientation:clockwise ? UIImageOrientationRight : UIImageOrientationLeft] drawInRect:CGRectMake(0,0,size.height ,size.width)]; 
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

return newImage; 
} 
+0

'在GPUImagePicture.m,向下滾動到 - (空)dealloc的;方法並添加行'[super dealloc];'最後。「這有幫助嗎? – Putz1103

+0

GPUImage是ARC,因此Xcode不會讓我添加該行。 – ORStudios

+0

從2012年開始,值得一試...... – Putz1103

回答

1

UIImage imageWithContentsOfFile:返回一個自動釋放物體,這將被釋放,只有當該應用返回到當前運行的循環。所以,可能是你的應用程序循環了很長時間來執行這段代碼?在這種情況下,autorelease對象可能會累積,並增加堆的使用。
如果是的話,你可以嘗試插入負責循環體自動釋放塊:

@autoreleasepool { 
...your statements 
}