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

查看:15
本文介绍了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

在 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天全站免登陆