IOS Photos应用程序如何在一个屏幕上显示数百张照片? [英] How can IOS Photos app can show hundreds of photos in one screen?

查看:158
本文介绍了IOS Photos应用程序如何在一个屏幕上显示数百张照片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on an application that relies on showing lots of photos on screen from multiple sources.

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

I am wondering how could apple make that "photo tile" view in their photo app? With that much photos, in my opinion the app should give memory warning with less photos (even if i load photos in "thumbnail size") displayed at once, but i can see that it can show hundreds of photos in one screen when i zoom out.

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

One way i can think of is these views are not individual photos but they are single 'tiled' images generated from photos on real time, so the app just shows 2-3 photos at once. But this still needs lots of cpu power and time to generate that fast. I can zoom in or out instantly .

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

I want to achieve similar functionality in my app, any directions on how to achieve this would be great.

感谢您的回答。

推荐答案

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

I have made some research and found that IOS8 has a wonderful new framework called "Photos Framework"

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

This Framework lets you easily cache images, call thumbnail images with predefined sizes and load items. (old ALAsset library just had one "thumbnail" size, you had to resize your own.)

我的测试屏幕上满20x20张照片(我的手机屏幕内容384图片),应用程序只占用10mb的内存,滚动时几乎没有闪烁。通过优化单元重新加载imo可以实现平滑滚动。

on my test a screen full of 20x20 photos (my phones screen content 384 images), app only takes 10mb's of memory, and almost no flickering when scrolling. Smooth scrolling can be achieved by optimizing cell reloading imo.

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

Heres the code i've used for loading images into a uicollectionview with 20x20 item size:

@import Photos;
@interface MainViewController ()

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

@end

on viewDidLoad:

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

和集合视图数据源方法:

and collection view datasource methods:

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

多数民众赞成!

这篇关于IOS Photos应用程序如何在一个屏幕上显示数百张照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆