如何在iOS上创build图库

我开始为iOS开发一个简单的应用程序,这个应用程序是一个简单的图片库(从网站上获取)。 我遇到的第一个问题是如何为图库创build视图。

视图应该是这样的(或照片应用程序): 示例视图

然而,这样做的观点是有问题的,首先是因为它使用固定的维度,我觉得有点难以实现(对我来说)。

另一种方法是在tableview中使用自定义单元格,如下所示: 定制单元格

但仍然使用固定维度。

什么是创build一个画廊,而不使用任何第三部分库(如Three20)的最佳途径?

感谢您的任何回复:)

PS。 我认为使用固定尺寸是不好的,因为新的iPhone 4(具有不同的分辨率),我说得对吗?

你应该看看AQGridView ,它正是你正在尝试实现的。 即使你想编写你自己的自定义代码,看看AQGridView的源代码很可能需要使用UIScrollView作为基础。

如果你想使用第三方类,下一个教程可以混合,他们为我工作。 这是一个很好的网格视图:
自定义图像select器像uiimagepicker

如果你想asynchronous加载它们,使用这个: image lazy loading

这两个教程都有很好的描述,并有源代码。

分辨率的差异不应该成为一个问题,因为如果我记得正确的话,如果iOS检测到它具有视网膜显示,则将UI组件和图像放大到正确的分辨率。 一边 请记住,如果您打算在不降低质量的情况下同时支持两种屏幕尺寸,请开始制作高清/低分辨率版本的graphics。

只要你devise点而不是像素(这是在XCode 4中完成的方式),iOS将能够透明地处理缩放。 在一个小屏幕上,一个点将是一个像素,而在视网膜显示器上将是两个像素。 这使得它能够在视网膜显示器上呈现清晰的外观。 资源

我知道这个问题是旧的,但我没有看到任何人解决固定宽度的问题,所以我认为我会贡献一次。

如果你不想使用第三方库,你应该在UITableView行中这样做。 由于UITableViewcaching单元格的方式,它在内存中相对较轻。 当然,比UIScrollView中可能的非常大的UIView更重要。 我已经做到了这一点,我对UITableView感到高兴。

这就是说,下一次我需要这样做? 我打算使用AQGridView。

嗯,自ios6出来以后,正确的方法是使用Collection Views:

关于CollectionView的Apple文档

另外,请参阅两个WWDC 2012会议:

集合视图简介
高级集合视图

可悲的是,苹果公司并没有包括一个简单的画廊或封面stream布局,但是做一个很容易。

我写了一篇关于使用UICollectionView构build媒体库的教程。 它从用户的照片库中填充。 我认为这对您正在尝试做的事情完美无缺。

iPhone编程教程:创build一个图像库像超过 – 第1部分

希望有所帮助。 干杯!

在我自己的一个项目中,我做了非常类似的事情。 我只是在这里显示代码的一部分,但是如果你想查看完整的代码,你可以在GitHub GitHub Repo上查看

首先我用ImageView制作了一个自定义的集合视图单元格

在CustomCollectionCell.h中

#import <UIKit/UIKit.h> @interface CustomCollectionCell : UICollectionViewCell @property (nonatomic , retain) UIImageView *imageView; @end 

在CustomCollectionCell.m中

 #import "CustomCollectionCell.h" @implementation CustomCollectionCell - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self setupImageView]; } return self; } #pragma mark - Create Subviews - (void)setupImageView { self.imageView = [[UIImageView alloc] initWithFrame:self.bounds]; self.imageView.autoresizingMask = UIViewAutoresizingNone;//UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [self addSubview:self.imageView]; } @end 

然后在你想要缩略图的视图中设置CollectionView

在ThumbNailViewController.m(片段)

 UICollectionView *collectionViewThumbnails; 

在ThumbNailViewController.m(片段)

 UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init]; collectionViewThumbnails=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 50) collectionViewLayout:layout]; if (collectionViewThumbnails && layout) { [collectionViewThumbnails setDataSource:self]; [collectionViewThumbnails setDelegate:self]; [collectionViewThumbnails registerClass:[CustomCollectionCell class] forCellWithReuseIdentifier:@"cellIdentifier"]; [collectionViewThumbnails setBackgroundColor:[UIColor blackColor]]; [self.view addSubview:collectionViewThumbnails]; } 

然后你有收集视图所需的方法。 在这里你可以设置你的东西

 //Number of items in the collectionview - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return [galleryData count]; } //Set up what each cell in the collectionview will look like //Here is where you add the thumbnails and the on define what happens when the cell is clicked - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { //initialize custom cell for the collectionview CustomCollectionCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; [cell.imageView setClipsToBounds:YES]; cell.imageView.contentMode = UIViewContentModeScaleAspectFill; //format url to load image from NSString *url = [NSString stringWithFormat:@"http://andrecphoto.weebly.com/uploads/6/5/5/1/6551078/%@",galleryData[indexPath.item]]; //load thumbnail [cell.imageView setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; //Sets up taprecognizer for each cell. (onlcick) UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; [cell addGestureRecognizer:tap]; //sets cell's background color to black cell.backgroundColor=[UIColor blackColor]; return cell; } //Sets size of cells in the collectionview - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(100, 100); } //Sets what happens when a cell in the collectionview is selected (onlclicklistener) - (void)handleTap:(UITapGestureRecognizer *)recognizer { //gets the cell thats was clicked CustomCollectionCell *cell_test = (CustomCollectionCell *)recognizer.view; //gets indexpath of the cell NSIndexPath *indexPath = [collectionViewThumbnails indexPathForCell:cell_test]; if (isConnectedGal) { //sets the image that will be displayed in the photo browser [photoGallery setInitialPageIndex:indexPath.row]; //pushed photobrowser [self.navigationController pushViewController:photoGallery animated:YES]; } } 

希望能回答你的问题。

这是一个非常好的图书馆,叫做FGallery for iOS

– 支持自动旋转

-thumbnail视图

-放大

-删除