ios - 由于存储在文档目录应用程序中的大量图像正在接收内存警告。应用程序崩溃了
<p><p>我正在开发一个高度依赖于将图像保存在文档目录中并检索它并将其显示在屏幕上的应用程序。</p>
<p>当我在 Collection View 中显示 5 -6 图像时,应用程序会变慢并突然收到内存警告并停止运行并且应用程序崩溃。</p>
<p>我正在使用以下代码来显示数据</p>
<pre><code>-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
</code></pre>
<p>{
DocumentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DocumentCollectionViewCell"
forIndexPath:indexPath];</p>
<pre><code>if(cell!=nil){
cell= ;
}
Documents *documents = ;
cell.imgView.contentMode = UIViewContentModeScaleAspectFit;
cell.imgContentView.layer.cornerRadius = 4.0f;
cell.imgContentView.layer.masksToBounds = YES;
cell.imgContentView.layer.borderColor = .CGColor;
cell.imgContentView.layer.borderWidth = .4f;
;
cell.btnLockOrUnlock.tag = indexPath.row;
// set count
cell.lblCount.text =;
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;
forState:UIControlStateNormal];
}
else{
forState:UIControlStateNormal];
cell.bluredView.hidden = YES;
}
;
cell.btnSelect.tag = indexPath.row;
NSString *title = documents.title;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = ; // Fetch path for document directory
NSString * docDirectoryPath = (NSMutableString *);
//-----------------path of document ------------------
NSString *filePath = ];
BOOL fileExists = [ fileExistsAtPath:filePath];
int i =0;
while (!fileExists) {
filePath = ];
fileExists = [ fileExistsAtPath:filePath];
i++;
}
CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef) , 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 = [ 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;
</code></pre>
<p>}</p>
<p>我将所有对象设置为零。</p>
<p>我正在使用以下代码保存图像</p>
<pre><code> NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = ; // Fetch path for document directory
folderName = (NSMutableString *);
</code></pre>
<p>NSData *pngData = UIImagePNGRepresentation(arrImages);
NSString *filePath = [文件夹名称 stringByAppendingPathComponent:];//添加文件名
;//写入文件</p>
<p>我将相机中的原始图像保存在文档目录中,没有进行任何压缩或调整大小。</p>
<p>我无法理解问题,请帮忙。</p>
<p>提前致谢。 </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>好像是内存泄漏,用instrument检查你的app,缺少工具。<br/></p>
<pre><code>CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef) , 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 = [ initWithCGImage:thumbnail];//<--CRASH
</code></pre>
<p>CFFoundation 对象遵循 ARC (<a href="https://stackoverflow.com/questions/31434411/does-the-core-foundation-objects-are-automatically-released-by-arc-or-do-we-need" rel="noreferrer noopener nofollow">ARC doesn't manage core foundation objects</a>) 之前的 Obj-C 类似的内存管理规则(如果创建或复制,则需要释放它)。<br/>
在您显示的代码中,我看到您没有释放 <code>CGImageRef</code> 和 <code>CGImageSourceRef</code>,这会造成两次泄漏,并可能导致崩溃。<br/>
Collection View 单元格被回收,因此在内存中打开的图像数量基本上就是您在屏幕上看到的单元格数量,它们应该是导致崩溃的原因。</p></p>
<p style="font-size: 20px;">关于ios - 由于存储在文档目录应用程序中的大量图像正在接收内存警告。应用程序崩溃了,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36519694/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36519694/
</a>
</p>
页:
[1]