首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用AssetsLibrary.framework因内存压力而终止

使用AssetsLibrary.framework因内存压力而终止
EN

Stack Overflow用户
提问于 2014-04-23 06:37:10
回答 2查看 550关注 0票数 0

我正在使用AssetsLibrary.framework从设备库中获取图像,我成功地从图片库中获取了所有图像并显示在我的桌子上,当我多次上下滚动时,我收到了一个内存问题警告,在控制台上打印出来,过了一段时间,它就崩溃了,因为内存压力而崩溃。

代码语言:javascript
复制
- (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;
}

所以我需要帮助找出错误的地方?谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-23 20:25:40

代码语言:javascript
复制
ALAssetRepresentation *representation = [asset defaultRepresentation];
image=[UIImage imageWithCGImage:[representation fullResolutionImage]];
[selectedAllImages addObject:image];
url = [representation url];

为什么要在fullResolutionImage中获得cellForRowAtIndexPath方法?并放入selectedAllImages数组..。selectedAllImages数组似乎被完全分辨率的图像无限大填充(在滚动期间)。

代码语言:javascript
复制
assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:imageURL
               resultBlock:resultblock
              failureBlock:nil];

为什么要创建资产库并请求相同的资产(其中有相同的url)?

我认为你应该简化你的'cellForRowAtIndexPath‘方法,以便在滚动过程中更轻量级。就像这样:

代码语言:javascript
复制
- (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;
}
票数 2
EN

Stack Overflow用户

发布于 2014-04-23 19:14:23

从资产库加载的图像将很大。如果您试图将其中许多内容加载到表视图中,那么很快就会耗尽内存。通常的做法是从较大的图像中创建缩放的图像,这些图像占用的内存要少得多。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23236649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档