Objective c UICollectionView/TableView Base64映像内存分配问题
我有一个UICollectionView/UITableView显示图像,当我滚动图像时,内存分配逐渐增加,最终导致设备崩溃 要为UICollectionView/UITableView创建我的对象,我有一个生成器类,在这个类中我从JSON生成我的对象(JSON中包含一个图像的Base64字符串)。创建这些对象后,我将它们放入一个NSArray,然后将它们传递给UICollectionView/UITableView 当(在builder类中)我从Base64创建UIImage时,会发生内存分配问题:Objective c UICollectionView/TableView Base64映像内存分配问题,objective-c,uitableview,memory-management,uiimage,uicollectionview,Objective C,Uitableview,Memory Management,Uiimage,Uicollectionview,我有一个UICollectionView/UITableView显示图像,当我滚动图像时,内存分配逐渐增加,最终导致设备崩溃 要为UICollectionView/UITableView创建我的对象,我有一个生成器类,在这个类中我从JSON生成我的对象(JSON中包含一个图像的Base64字符串)。创建这些对象后,我将它们放入一个NSArray,然后将它们传递给UICollectionView/UITableView 当(在builder类中)我从Base64创建UIImage时,会发生内存分配
NSData *dataFromString = [[NSData alloc] initWithBase64EncodedString:base64 options:0];
UIImage *image = [[UIImage alloc]initWithData:dataFromString];
但是,如果我从png/jpg(同样在builder类中)创建UIImage,我没有问题(即内存分配没有增长):
我使用了仪器,似乎没有内存泄漏,当我关闭UICollectionView/UITableView时,内存占用会减少,但我不知道为什么使用Base64会有不同
提前谢谢
卡尔。我通过创建一个tempFileManger类解决了这个问题,然后将UIImages作为PNG写入一个临时目录:
NSString *path = [tempDir stringByAppendingPathComponent:imageObject.title];
NSData *data = UIImagePNGRepresentation(imageObject.image);
然后在我的cellForItemAtIndexPath方法中,我使用以下方法检索:
NSString *path = [tempDir stringByAppendingPathComponent:imageObject.title];
UIImage *image = [UIImage imageWithContentsOfFile:path];
使用完这些图像后,我会使用tempFileManagerClass删除temp文件夹中的所有图像
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:tempDir error:nil];
for (NSString *filename in contents) {
[[NSFileManager defaultManager] removeItemAtPath:[tempDir stringByAppendingPathComponent:filename] error:NULL];
}
我将尝试
initWithContentsOfFile
,而不是imageNamed
。imageNamed
函数添加了缓存和一些额外的内存管理功能,对于比较内存使用情况并不太好。。。将代码更改为“initWithContentsOfFile”确实会在我的应用程序中导致相同的内存增长。这给了我一些其他的调查谢谢。。。
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:tempDir error:nil];
for (NSString *filename in contents) {
[[NSFileManager defaultManager] removeItemAtPath:[tempDir stringByAppendingPathComponent:filename] error:NULL];
}