【问题标题】:Reloading UICollectionView With UIImages not Releasing Memory使用 UIImage 重新加载 UICollectionView 不会释放内存
【发布时间】:2014-02-21 16:56:16
【问题描述】:

我使用以下代码在图像周围创建边框。我循环遍历以下多次以创建带有边框的缩略图图像,然后将其放置在 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;
}

【问题讨论】:

  • In GPUImagePicture.m, scroll down to the -(void)dealloc; method and add the line '[super dealloc];' at the end. 有帮助吗?
  • GPUImage 是 ARC,所以 Xcode 不允许我添加该行。
  • 这是 2012 年的作品,值得一试……

标签: ios objective-c memory uiimage gpuimage


【解决方案1】:

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

@autoreleasepool {
...your statements
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-02
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多