2010-10-05 176 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]; 
    } 
} 

希望能回答你的問題。