Alassetslibrary 使用AssetsLibrary.framework由于内存压力而终止
我正在使用AssetsLibrary.framework从设备库中获取图像。我成功地从库中获取了所有图像,并显示在我的表视图中。当我上下滚动多次时,会出现问题,我在控制台上收到一条内存问题警告,经过一段时间后,它会崩溃,说由于内存压力而崩溃Alassetslibrary 使用AssetsLibrary.framework由于内存压力而终止,alassetslibrary,ios7.1,alassetsgroup,Alassetslibrary,Ios7.1,Alassetsgroup,我正在使用AssetsLibrary.framework从设备库中获取图像。我成功地从库中获取了所有图像,并显示在我的表视图中。当我上下滚动多次时,会出现问题,我在控制台上收到一条内存问题警告,经过一段时间后,它会崩溃,说由于内存压力而崩溃 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *Cell
- (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;
}
所以我需要帮助,错误在哪里?谢谢您从资产库加载的图像将会很大。如果您试图在表视图中加载其中的许多内容,那么很快就会耗尽内存。通常的做法是从使用更少内存的较大图像创建缩放图像
ALAssetRepresentation *representation = [asset defaultRepresentation];
image=[UIImage imageWithCGImage:[representation fullResolutionImage]];
[selectedAllImages addObject:image];
url = [representation url];
为什么在cellForRowAtIndexPath方法中获得完全分辨率图像?并将其放入selectedAllImages数组中。。似乎selectedAllImages数组由fullresolutionimages infinity(滚动期间)填充
为什么创建AssetLibrary并请求相同的资产(具有相同的url)
我认为您应该简化“CellForRowatineXpath”方法,以便在滚动过程中更加轻量级。大概是这样的:
- (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;
}
谢谢Aleksey…..您的建议真的很有帮助,我接受了您的回答。但我还有一个疑问,即我需要将所有UIImage保存在NSMutableArray中,我的操作如下-->image=[UIImage imageWithCGImage:[representation FullResolutioniImage]];[selectedAllImages添加对象:图像];。您能告诉我如何将所有UIImage存储在NSMutableArray中吗?因为如果我使用这两行-->image=[UIImage imageWithCGImage:[representation fullResolutionImage]];[selectedAllImages添加对象:图像];,我将收到内存问题警告。如果要存储所有UIImage,请将此循环放在某个好位置(viewDidLoad或其他-而不是CellForRowatineXpath):对于(图像中的ALAsset*资产){UIImage*图像=[UIImage imageWithCGImage:[[asset defaultRepresentation]fullResolutionImage]];[selectedAllImages addObject:image]}但我不建议这样做,因为fullResolutionImages可能会很大,而且如果你保留的fullResolutionImages很少,应用程序可能会因为内存压力而被杀死。是的。我的要求是我想将所有UIImage保存在一个NSMutableArray中。我该怎么做呢。
- (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;
}