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

Ios 由于文件目录中存储了大量图像,应用程序正在接收内存警告。应用程序崩溃了,ios,iphone,memory-management,out-of-memory,nsdocumentdirectory,Ios,Iphone,Memory Management,Out Of Memory,Nsdocumentdirectory,我正在开发一个应用程序,它高度依赖于在文档目录中保存图像,检索图像并将其显示在屏幕上 当我在采集视图中显示5-6幅图像时,ap就会变慢,并突然收到内存警告,停止运行,应用程序崩溃 我使用以下代码来显示数据 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { Document

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

当我在采集视图中显示5-6幅图像时,ap就会变慢,并突然收到内存警告,停止运行,应用程序崩溃

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

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
             cellForItemAtIndexPath:(NSIndexPath *)indexPath
{ DocumentCollectionViewCell*单元格=[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;
NSData*pngData=UIImagePNGRepresentation(arrImages[i]); NSString*filePath=[folderName stringByAppendingPathComponent:[NSString stringWithFormat:@“image%d.png”,i]];//添加文件名 [pngData writeToFile:filePath原子:是];//写入文件

我从相机中保存原始图像,无需在文档目录中进行任何压缩或调整大小

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


提前感谢。

这似乎是由于内存泄漏,请使用仪器和工具检查您的应用程序。

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
CGImageSourceRef src=CGImageSourceCreateWithURL((uu桥CFURLRef)[NSURL fileURLWithPath:filePath],NULL);
//创建缩略图选项
CFDictionaryRef选项=(uu桥CFDictionaryRef)@{
(id)KCGimageSourceCreateTumbNailWithTransform:@是,
(id)KCGIMAGESourceCreateTumbnailFromImageAlways:@是,
(id)kCGImageSourceThumbnailMaxPixelSize:@(cell.imgView.frame.size.height)
};
//生成缩略图
CGImageRef缩略图=CGImageSourceCreateTumbnailAtIndex(src,0,选项);

UIImage*UIImage=[[UIImage alloc]initWithCGImage:thumbnail];//谢谢Andrea它帮助了我:)
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