IOS Photos应用程序如何在一个屏幕上显示数百张照片?

dre*_*der 9 objective-c ios uicollectionview

我正在开发一个依赖于从多个来源在屏幕上显示大量照片的应用程序.

我想知道苹果如何在他们的照片应用程序中制作"照片拼贴"视图?有这么多的照片,在我看来应用程序应该提供内存警告,一次显示较少的照片(即使我加载"缩略图大小"的照片),但我可以看到它可以在我缩放时在一个屏幕上显示数百张照片出.

我能想到的一种方式是这些视图不是单独的照片,而是实时从照片生成的单个"平铺"图像,因此应用程序只显示2-3张照片.但是这仍然需要大量的CPU功率和时间来快速生成.我可以立即放大或缩小.

我想在我的应用程序中实现类似的功能,如何实现这一点的任何方向都会很棒.

谢谢你的回答.

IOS 7 Photos.app

dre*_*der 11

我做了一些研究,发现IOS8有一个很棒的新框架叫做"照片框架"

此框架可让您轻松缓存图像,调用具有预定义大小的缩略图图像并加载项目.(旧的ALAsset库只有一个"缩略图"大小,你必须调整自己的大小.)

在我的测试中,一个充满20x20张照片的屏幕(我的手机屏幕内容384张图片),app只需要10mb的内存,滚动时几乎没有闪烁.通过优化单元重新加载imo可以实现平滑滚动.

下面是用于将图像加载到20x20项目大小的uicollectionview中的代码:

@import Photos;
@interface MainViewController ()

@property (strong) PHFetchResult *assetsFetchResults;
@property (strong) PHCachingImageManager* imageManager;

@end
Run Code Online (Sandbox Code Playgroud)

在viewDidLoad上:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.imageManager = [[PHCachingImageManager alloc] init];

    CGFloat scale = [UIScreen mainScreen].scale;
    CGSize cellSize = ((UICollectionViewFlowLayout *)self.collectionViewLayout).itemSize;
    AssetGridThumbnailSize = CGSizeMake(cellSize.width * scale, cellSize.height * scale);

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    self.assetsFetchResults = [PHAsset fetchAssetsWithOptions:nil];
    // Do any additional setup after loading the view.
}
Run Code Online (Sandbox Code Playgroud)

和集合视图数据源方法:

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.assetsFetchResults.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    [self.imageManager requestImageForAsset:self.assetsFetchResults[indexPath.item] 
                                 targetSize:CGSizeMake(20, 20) 
                                contentMode:PHImageContentModeAspectFill 
                                    options:nil resultHandler:^(UIImage *result, NSDictionary* info){

                                        UIImageView* imgView = (UIImageView*)[cell.contentView viewWithTag:999];
                                        if(!imgView) {
                                            imgView = [[UIImageView alloc] initWithFrame:[cell.contentView bounds]];
                                            imgView.tag = 999;
                                            [cell.contentView addSubview:imgView];
                                        }
                                        imgView.image = result;
                                    }];
    // Configure the cell

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

而已!

  • 我不认为您使用PHCachingImageManger缓存任何图像.为了缓存图像,您需要在PHCachingImageManager上调用此方法,startCachingImagesForAssets(_:targetSize:contentMode:options :) (3认同)