gpt4 book ai didi

ios - 当我在滚动 UITableview 期间显示文档目录中的图像时,内存不断增加然后应用程序崩溃

转载 作者:可可西里 更新时间:2023-11-01 05:33:47 25 4
gpt4 key购买 nike

我的要求是下载应用程序内存中的所有图像,并在可用时从本地显示。

下面是我从本地访问图像的代码,如果它不可用,那么它将下载然后显示。

[cell.imgProfilePic processImageDataWithURLString:cData.PICTURE];

我制作了自定义的 UIImageView

DImageView.h

    #import <UIKit/UIKit.h>
@interface DImageView : UIImageView
@property (nonatomic, strong) UIActivityIndicatorView *activityView;
- (void)processImageDataWithURLString:(NSString *)urlString;
+ (UIImage *)getSavedImage :(NSString *)fileName;
@end

DImageView.m

#import "DImageView.h"
#define IMAGES_FOLDER_NAME @"DImages"

@implementation DImageView

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{ }
return self;
}
- (void)dealloc
{
self.activityView = nil;
[super dealloc];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self initWithFrame:[self frame]];
}
return self;
}
- (void)processImageDataWithURLString:(NSString *)urlString
{
@autoreleasepool
{
UIImage * saveImg =[DImageView getSavedImage:urlString];
if (saveImg)
{
@autoreleasepool
{
dispatch_queue_t callerQueue = dispatch_get_main_queue();
dispatch_async(callerQueue, ^{

@autoreleasepool{
[self setImage:saveImg];
}
});
}
}
else
{
[self showActivityIndicator];
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

dispatch_queue_t callerQueue = dispatch_get_main_queue();
dispatch_queue_t downloadQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
__block NSError* error = nil;
dispatch_async(downloadQueue, ^{

@autoreleasepool
{
NSData * imageData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
if (!error)
{
dispatch_async(callerQueue, ^{
@autoreleasepool {
UIImage *image = [UIImage imageWithData:imageData];
[self setImage:image];
[self hideActivityIndicator];
[self saveImageWithFolderName:IMAGES_FOLDER_NAME AndFileName:urlString AndImage:imageData];
}
});
}
}
});
dispatch_release(downloadQueue);
}
}
}
- (void) showActivityIndicator
{
self.activityView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.activityView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
self.activityView.hidesWhenStopped = TRUE;
self.activityView.backgroundColor = [UIColor clearColor];
self.activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;

[self addSubview:self.activityView];
[self.activityView startAnimating];
}
- (void) hideActivityIndicator
{
CAAnimation *animation = [NSClassFromString(@"CATransition") animation];
[animation setValue:@"kCATransitionFade" forKey:@"type"];
animation.duration = 0.4;;
[self.layer addAnimation:animation forKey:nil];
[self.activityView stopAnimating];
[self.activityView removeFromSuperview];

for (UIView * view in self.subviews)
{
if([view isKindOfClass:[UIActivityIndicatorView class]])
[view removeFromSuperview];
}
}
- (void)saveImageWithFolderName:(NSString *)folderName AndFileName:(NSString *)fileName AndImage:(NSData *) imageData
{
@autoreleasepool{
NSFileManager *fileManger = [[NSFileManager defaultManager] autorelease];
NSString *directoryPath = [[NSString stringWithFormat:@"%@/%@",[DImageView applicationDocumentsDirectory],folderName] autorelease];

if (![fileManger fileExistsAtPath:directoryPath])
{
NSError *error = nil;
[fileManger createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:&error];
}
fileName = [DImageView fileNameValidate:fileName];
NSString *filePath = [[NSString stringWithFormat:@"%@/%@",directoryPath,fileName] autorelease];

BOOL isSaved = [imageData writeToFile:filePath atomically:YES];
if (!isSaved)DLog(@" ** Img Not Saved");
}
}

+ (NSString *)applicationDocumentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}

+ (UIImage *)getSavedImage :(NSString *)fileName
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
fileName = [DImageView fileNameValidate:fileName];

NSFileManager * fileManger = [[NSFileManager defaultManager] autorelease];
NSString * directoryPath = [[NSString stringWithFormat:@"%@/%@",[DImageView applicationDocumentsDirectory],IMAGES_FOLDER_NAME] autorelease];
NSString * filePath = [[NSString stringWithFormat:@"%@/%@",directoryPath,fileName] autorelease];

if ([fileManger fileExistsAtPath:directoryPath])
{
UIImage *image = [[[UIImage imageWithContentsOfFile:filePath] retain]autorelease];
if (image)
return image;
else
return nil;
}
[pool release];
return nil;
}

+ (NSString*) fileNameValidate : (NSString*) name
{
name = [name stringByReplacingOccurrencesOfString:@"://" withString:@"##"];
name = [name stringByReplacingOccurrencesOfString:@"/" withString:@"#"];
name = [name stringByReplacingOccurrencesOfString:@"%20" withString:@""];
return name;
}
@end

一切正常,平滑滚动以及后台异步图像下载。

问题是当我滚动 UITableview 应用程序内存不断增加,一段时间后我得到 Receive memory waring 2/3 时间然后应用程序崩溃。

当我使用 AsyncImageView 类时,内存不会增加并且工作正常。但由于应用程序要求,我将所有图像保存到 Document Directory 并在可用时从中显示。

我已经尝试使用 @autoreleasepoolrelease 一些变量但没有成功。

如果有人有管理内存管理的解决方案,我将不胜感激。

 **ARC is off in my application.** 

最佳答案

UIImagePNGRepresentation 可能会返回非自动释放的对象 - 您可以尝试释放它并查看是否会导致崩溃。显然你没有发布任何东西,但除了图像表示之外没有什么是显而易见的。

一些其他评论:

  • 使用 ObjectAlloc 工具在 Instruments 中运行您的应用程序,应该会立即清楚哪些对象没有被释放。如果您不了解 Instruments,那么现在是时候学习它了。

  • 您可以“跟踪”对象并在使用 ObjectTracker 释放对象时收到消息- 但是它是为 ARC 设计的,因此您可能需要对其进行调整。如果您使用它,您会在每个对象被释放时看到一条消息

  • 当表格 View 完成一个单元格时,您可以接收一个委托(delegate)方法来告诉您,然后您可以 nil(释放)单元格保留的对象

  • 您对 downloadQueue 的使用很奇怪 - 在您的实例中将其作为 ivar 创建一次,根据需要使用它,然后在 dealloc 中释放它

  • 您将事件微调器隐藏在主队列中,但不要在主队列中启动它

  • 您命令事件 View 将其自身从其父 View 中移除,但随后在 subview 中查找并尝试将其移除:

[self.activityView removeFromSuperview];

for (UIView * view in self.subviews)
{
if([view isKindOfClass:[UIActivityIndicatorView class]])
[view removeFromSuperview];
}

最后,Instruments 就是您想要的。你可以在这里阅读更多关于它的信息,或者只是谷歌,你一定会找到大量的博客来阅读。

关于ios - 当我在滚动 UITableview 期间显示文档目录中的图像时,内存不断增加然后应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29017579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com