使用UIImages重新加载UICollectionView不释放内存

时间:2014-02-21 16:56:17

标签: ios objective-c memory uiimage gpuimage

我使用以下代码在图像周围创建边框。我循环多次以创建带有边框的缩略图,然后将其放在UICollectionView中。

问题似乎是每次重新加载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;
}

1 个答案:

答案 0 :(得分:1)

UIImage imageWithContentsOfFile:返回一个autorelease对象,该对象仅在应用程序返回当前运行循环时释放。那么,可能是你的应用程序循环执行此代码很长一段时间?在这种情况下,自动释放对象可能会累积,并增加堆使用量 如果是这样,你可以尝试在负责的循环体中插入一个自动释放块:

@autoreleasepool {
...your statements
}
相关问题