由于存储在文档目录app中的大量图像正在接收内存警告。应用程序崩溃了

时间:2016-04-09 16:35:40

标签: ios iphone memory-management out-of-memory nsdocumentdirectory

我正在开发一个应用程序,它非常依赖于在文档目录中保存图像并检索它并在屏幕上显示它。

一旦我在集合视图中显示5 -6个图像,ap就会变慢并突然收到内存警告并停止运行和应用程序崩溃。

我正在使用以下代码来显示数据

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
             cellForItemAtIndexPath:(NSIndexPath *)indexPath

{     DocumentCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@" DocumentCollectionViewCell"                                                                      forIndexPath:indexPath];

if(cell!=nil){
    cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"DocumentCollectionViewCell" forIndexPath:indexPath];

}
Documents *documents = [arrTableData objectAtIndex:indexPath.row];
cell.imgView.contentMode = UIViewContentModeScaleAspectFit;
cell.imgContentView.layer.cornerRadius = 4.0f;
cell.imgContentView.layer.masksToBounds = YES;
cell.imgContentView.layer.borderColor = [UIColor lightGrayColor].CGColor;
cell.imgContentView.layer.borderWidth = .4f;

[cell.btnLockOrUnlock addTarget:self action:@selector(lockAction:) forControlEvents:UIControlEventTouchUpInside];
cell.btnLockOrUnlock.tag = indexPath.row;
// set count
cell.lblCount.text =[NSString stringWithFormat:@"%@ Page",documents.imageCount];
cell.imgView.layer.cornerRadius = 6.0f;
cell.imgView.layer.masksToBounds = YES;
newDocDate = documents.issueDate;
// set image
NSString * passcode = documents.passcode;
if(passcode.length>3){
    cell.bluredView.hidden = NO;
    [cell.btnLockOrUnlock setImage:[UIImage imageNamed:@"lockWhite"] forState:UIControlStateNormal];
}
else{
    [cell.btnLockOrUnlock setImage:[UIImage imageNamed:@"unlockWhite"] forState:UIControlStateNormal];
    cell.bluredView.hidden = YES;
}

[cell.btnSelect addTarget:self action:@selector(cellSelectAction:) forControlEvents:UIControlEventTouchUpInside];
cell.btnSelect.tag = indexPath.row;
NSString *title = documents.title;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Fetch path for document directory
NSString * docDirectoryPath = (NSMutableString *)[documentsDirectory stringByAppendingPathComponent:title];

//-----------------path of document ------------------

NSString *filePath = [docDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"image%d.png",0]];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
int i =0;
while (!fileExists) {
    filePath = [docDirectoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"image%d.png",i]];
    fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
    i++;
}
CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef) [NSURL fileURLWithPath:filePath], NULL);
// Create thumbnail options
CFDictionaryRef options = (__bridge CFDictionaryRef) @{
                                                       (id) kCGImageSourceCreateThumbnailWithTransform : @YES,
                                                       (id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
                                                       (id) kCGImageSourceThumbnailMaxPixelSize : @(cell.imgView.frame.size.height)
                                                       };
// Generate the thumbnail
CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src, 0, options);
UIImage* uiImage = [[UIImage alloc] initWithCGImage:thumbnail];  //<--CRASH
cell.DocName.text = documents.docName;


//-----------------display image on cell------------------


cell.imgView.image = uiImage;
uiImage = nil;
uiImage = NULL;
documents = nil;
documents = nil;
title = nil;
thumbnail = nil;
src = nil;
options = nil;
filePath = nil;
paths = nil;
documentsDirectory = nil;
docDirectoryPath = nil;
return cell;

}

我将所有对象设置为nil。

我使用以下代码保存图片

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Fetch path for document directory
folderName = (NSMutableString *)[documentsDirectory stringByAppendingPathComponent:folderName];

NSData * pngData = UIImagePNGRepresentation(arrImages [i]);             NSString * filePath = [folderName stringByAppendingPathComponent:[NSString stringWithFormat:@&#34; image%d.png&#34;,i]]; //添加文件名             [pngData writeToFile:filePath atomically:YES]; //写文件

我从相机保存原始图像,没有任何压缩或在文档目录中调整大小。

我无法理解这个问题,请帮忙。

提前致谢。

1 个答案:

答案 0 :(得分:1)

这似乎是由于内存泄漏,使用工具检查您的应用程序并缺少工具。

CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef) [NSURL fileURLWithPath:filePath], NULL);
// Create thumbnail options
CFDictionaryRef options = (__bridge CFDictionaryRef) @{
                                                       (id) kCGImageSourceCreateThumbnailWithTransform : @YES,
                                                       (id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
                                                       (id) kCGImageSourceThumbnailMaxPixelSize : @(cell.imgView.frame.size.height)
                                                       };
// Generate the thumbnail
CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src, 0, options);
UIImage* uiImage = [[UIImage alloc] initWithCGImage:thumbnail];  //<--CRASH

CFFoundation对象遵循ARC(ARC doesn't manage core foundation objects)之前的Obj-C类似的内​​存管理规则(如果您创建或复制,则需要释放它)。
在您显示的代码中,我发现您没有发布CGImageRefCGImageSourceRef,这会造成两次泄漏,可能还会发生崩溃。 收集视图单元格被回收,因此在内存中打开的图像数量基本上是您在屏幕上看到的单元格数量,它们应该是导致崩溃的原因。