我正在使用AssetsLibrary.framework从设备库中获取图像,我成功地从图片库中获取了所有图像并显示在我的桌子上,当我多次上下滚动时,我收到了一个内存问题警告,在控制台上打印出来,过了一段时间,它就崩溃了,因为内存压力而崩溃。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
ALAsset *asset = [self.images objectAtIndex:indexPath.row];
ALAssetRepresentation *representation = [asset defaultRepresentation];
image=[UIImage imageWithCGImage:[representation fullResolutionImage]];
[selectedAllImages addObject:image];
url = [representation url];
NSURL *imageURL=url;
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *representation = [myasset defaultRepresentation];
fileName = [representation filename];
cell.cellLabel.text=fileName;
};
assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:imageURL
resultBlock:resultblock
failureBlock:nil];
[cell.cellImageView setImage:[UIImage imageWithCGImage:[asset thumbnail]]];
return cell;
}所以我需要帮助找出错误的地方?谢谢
发布于 2014-04-23 20:25:40
ALAssetRepresentation *representation = [asset defaultRepresentation];
image=[UIImage imageWithCGImage:[representation fullResolutionImage]];
[selectedAllImages addObject:image];
url = [representation url];为什么要在fullResolutionImage中获得cellForRowAtIndexPath方法?并放入selectedAllImages数组..。selectedAllImages数组似乎被完全分辨率的图像无限大填充(在滚动期间)。
assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:imageURL
resultBlock:resultblock
failureBlock:nil];为什么要创建资产库并请求相同的资产(其中有相同的url)?
我认为你应该简化你的'cellForRowAtIndexPath‘方法,以便在滚动过程中更轻量级。就像这样:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
ALAsset *asset = [self.images objectAtIndex:indexPath.row];
ALAssetRepresentation *representation = [asset defaultRepresentation];
cell.cellLabel.text = [representation filename];
cell.cellImageView.image = [UIImage imageWithCGImage:[asset thumbnail]];
return cell;
}发布于 2014-04-23 19:14:23
从资产库加载的图像将很大。如果您试图将其中许多内容加载到表视图中,那么很快就会耗尽内存。通常的做法是从较大的图像中创建缩放的图像,这些图像占用的内存要少得多。
https://stackoverflow.com/questions/23236649
复制相似问题