Ios 由于多线程代码导致内存压力而终止
我的应用程序在使用过程中的某一点由于内存压力而终止,我已将问题隔离到导致问题的一段代码中 我将复制下面的代码(我已经通过注释此代码并运行应用程序进行了测试,应用程序运行良好) 我在这里尝试做的是:我正在阅读RSS提要,获取图像的URL并将其显示在tableView单元格中。下面的代码来自CellForRowatineXpathIos 由于多线程代码导致内存压力而终止,ios,multithreading,uitableview,Ios,Multithreading,Uitableview,我的应用程序在使用过程中的某一点由于内存压力而终止,我已将问题隔离到导致问题的一段代码中 我将复制下面的代码(我已经通过注释此代码并运行应用程序进行了测试,应用程序运行良好) 我在这里尝试做的是:我正在阅读RSS提要,获取图像的URL并将其显示在tableView单元格中。下面的代码来自CellForRowatineXpath // 1. Check the image cache to see if the image already exists. If so, then use it. I
// 1. Check the image cache to see if the image already exists. If so, then use it. If not, then download it.
if ([[ImageCache sharedImageCache] DoesExist:imgSrcString] == true)
{
cell.cellImageView.image = [[ImageCache sharedImageCache] GetImage:imgSrcString];
} else {
cell.cellImageView.image = [UIImage imageNamed:@"NoImage_Small"];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
// Now, we can’t cancel a block once it begins, so we’ll use associated objects and compare
// index paths to see if we should continue once we have a resized image.
objc_setAssociatedObject(cell,
kIndexPathAssociationKey,
indexPath,
OBJC_ASSOCIATION_RETAIN);
dispatch_async(queue, ^{
NSData * imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgSrcString]];
UIImage *image = [UIImage imageWithData:imageData];
//UIImage *resizedImage = [image scaleToSize:CGSizeMake(71.0f,71.0f)] ; //[UIImage resizeImage];
dispatch_async(dispatch_get_main_queue(), ^{
NSIndexPath *cellIndexPath =
(NSIndexPath *)objc_getAssociatedObject(cell, kIndexPathAssociationKey);
if(image){
if ([indexPath isEqual:cellIndexPath]) {
[[cell cellImageView] setImage:image];
[[ImageCache sharedImageCache] AddImage:imgSrcString :image];
}
}
// [[ImageCache sharedImageCache] AddImage:[imgSrcString stringByAppendingString:@"bigImage"] :image];
});
});
}
如果能帮我指出正确的方向,我将不胜感激。谢谢你的时间 关于这个问题,我已经上网了。顺便说一句,在他们提到的一些博客中,默认情况下,NSURLConnection包含一个根本不清楚的缓存。这可能就是坠机的原因。因此,尝试使用AsittpRequest,这样您就不会感觉到有关图像下载的内存警告 将此代码用于ASIHTTPRequest集成(添加sdk文件后)
我认为主要的问题不是代码中的多线程,而是我正在下载大图像并将它们存储在ImageCache中,这很快变得臃肿并导致崩溃。我解决这个问题的方法是在下载图像后调整图像大小,然后将较小的图像添加到缓存中(我将其用作缩略图,因此不需要存储较大的图像)。这大大减少了车祸的数量。希望它能帮助类似情况的人 您的项目已启用ARC,对吗?是的,它已启用ARC。内存泄漏将发生在哪一行?能用仪器跟踪吗?我一直在尝试用仪器跟踪它。我得到的只是泄漏的内存地址。由于这是我第一次使用仪器,我不确定如何获取导致仪器泄漏的相关代码。哦,好的,根据您的代码块,您没有手动分配内存,这是y询问的。如果您准备使用AsitpRequest更改图像下载功能,我将帮助您解决此问题。感谢您的努力。我将尝试使用它。ASIHTTPRequest代码不再被积极开发或维护,因此它作为解决方案的使用应该作为一个例子,但不是最好的方法
-(void)resetNetworkQueue
{
// initialising network queue.
self.networkQueue =[[ASINetworkQueue alloc]init];
[self.networkQueue setRequestDidFinishSelector:@selector(mediaDownloadCompleted:)];
[self.networkQueue setRequestDidFailSelector:@selector(mediaDownloadFailed:)];
[self.networkQueue setRequestDidStartSelector:@selector(mediaDownloadStarted:)];
[self.networkQueue setQueueDidFinishSelector:@selector(networkQueueCompletedProcess:)];
[self.networkQueue setMaxConcurrentOperationCount:10];
[self.networkQueue setShouldCancelAllRequestsOnFailure:NO];
[self.networkQueue setDelegate:self];
}
-(void)createDownloadRequest
{
//Adding request in network queue.
self.request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:imageURL]];
[self.request setDownloadDestinationPath:downloadDestinationPath];
[self.request setDelegate:self];
[self.request setAllowResumeForFileDownloads:YES];
[self.request setTimeOutSeconds:30];
[self.networkQueue addOperation:self.request];
[self.request setUserInfo:[NSDictionary dictionaryWithObjectsAndKeys:homeInfo,@"FloorPlanInfo",imageType,@"ImageType", nil]];
}
- (void)mediaDownloadStarted:(ASIHTTPRequest *)request
{
//Fire when request started.
}
- (void)mediaDownloadCompleted:(ASIHTTPRequest *)request
{
//Fire when request completed.
}
- (void)mediaDownloadFailed:(ASIHTTPRequest *)request
{
//Fire when request Failed to download.
}