2010-10-05 189 views
30

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

的观点应该是这样的(或照片应用程序): Sample View

但是做一个视图这种方式是有问题的,首先是因为它使用固定的尺寸,我觉得是有点难以实施(为了我)。

另一种方法是使用的tableview内的自定义单元格,就像这样: custom cell

,但它仍然使用固定尺寸。

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

感谢您的回复:)

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

回答

36

你应该检查出AQGridView这确实是你想要实现的。即使你想编写自己的自定义代码,看看AQGridView源代码很可能需要使用UIScrollView作为基础。

2

如果您不想使用第三方库,您应该在UITableView行中执行此操作。由于UITableView缓存单元格的方式,它在内存中相对较轻。当然,这比UIScrollView中可能非常大的UIView更重要。我已经完成了这两种方式,并且我对UITableView感到高兴。

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

6

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

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

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

2

嗯,因为iOS6的走了出来,这样做的正确方法是收集意见:

Apple Docs on CollectionViews

而且,看到两个WWDC上他们2012和会话:

Introduction to Collection Views
Advanced Collection Views不幸的是,苹果公司并没有包括一个简单的图库或封面流布局,但是制作一个很容易。

+0

我会检查一下。它适用于iOS 5吗? – patrick 2013-02-01 13:00:46

+2

只有第6号,但至少你应该指向那个方向。 – Rob 2013-02-01 16:53:29

+0

UICollectionViews的画廊布局的任何指针? – 2013-03-20 06:36:05

2

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

iPhone Programming Tutorial: Creating An Image Gallery Like Over – Part 1

希望有所帮助。干杯!

+0

谢谢。虽然这个问题实际上是在两年前发布的,但您的帖子仍然有用。顺便提一下,最好在这里发布完整的解决方案。 – patrick 2013-08-14 14:39:16

1

这里是一个叫FGallery为iOS

- 支持自动旋转

-thumbnail查看

-zoom

-delete

2

我做了一件非常非常好库类似于我自己的一个项目。我只显示代码的某些部分在这里,但如果你想查看完整的代码,你可以查看它在GitHub GitHub Repo

首先,我在CustomCollectionCell.h

一个定制集合视图细胞与ImageView的

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

希望能回答你的问题。