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

我正在开发一个应用程序,它依靠在多个来源的屏幕上显示大量照片。

我想知道苹果如何能够在他们的照片应用程序“照片瓷砖”视图? 有了这么多的照片,在我看来,应用程序应该提供更less的照片的内存警告(即使我加载照片的“缩略图大小”)一次显示,但我可以看到,它可以显示数百张照片,当我放大出。

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

我想在我的应用程序中实现类似的function,如何实现这一目标的任何方向将是伟大的。

感谢您的回答。

IOS 7 Photos.app

我做了一些研究,发现IOS8有一个很好的新架构,叫做“Photos Framework”

通过此框架,您可以轻松地caching图像,调用预定义大小的缩略图图像和加载项目。 (旧的ALAsset库只有一个“缩略图”大小,你必须调整自己的大小。)

在我的testing屏幕上充满了20×20的照片(我的手机屏幕内容为384张图片),app只需要10mb的内存,滚动时几乎不闪烁。 平滑滚动可以通过优化单元重新加载imo来实现。

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

@import Photos; @interface MainViewController () @property (strong) PHFetchResult *assetsFetchResults; @property (strong) PHCachingImageManager* imageManager; @end 

on 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. } 

和集合视图数据源方法:

 #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; } 

而已!

苹果使用PHCachingImageManager的例子好多了,因为它也使用预热文本,以便它caching正确的资源。 请检查出来https://developer.apple.com/library/ios/samplecode/UsingPhotosFramework/Listings/SamplePhotosApp_AAPLAssetGridViewController_m.html对不起,格式不好,希望它可以帮&#x52A9;