【问题标题】:How To Create A Gallery on iOS如何在 iOS 上创建图库
【发布时间】:2010-10-05 18:50:14
【问题描述】:

我开始为 iOS 开发一个简单的应用程序,这个应用程序是一些照片的简单画廊(取自网站)。 我遇到的第一个问题是如何为画廊创建视图。

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

但是以这种方式进行视图是有问题的,首先因为它使用固定尺寸,而且我认为实现起来有点困难(对我来说)。

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

但它仍然使用固定尺寸。

在不使用任何第三方库(如 Three20)的情况下创建画廊的最佳方式是什么?

感谢您的回复:)

PS。我认为由于新的 iphone 4(分辨率不同),使用固定尺寸不好,对吗?

【问题讨论】:

    标签: iphone objective-c ios


    【解决方案1】:

    您应该查看AQGridView,这正是您想要实现的目标。即使您想编写自己的自定义代码,也请查看 AQGridView 源代码,因为您很可能需要使用 UIScrollView 作为基础。

    【讨论】:

      【解决方案2】:

      如果您想使用第三方课程,可以混合下一个教程,它们对我有用。 这是一个很好的网格视图:
      custom image picker like uiimagepicker

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

      两个教程都描述得很好并且有源代码。

      【讨论】:

        【解决方案3】:

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

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

        我知道这个问题很老了,但我没有看到有人解决固定宽度的问题,所以我想我会贡献一次。

        【讨论】:

          【解决方案4】:

          如果您不想使用第三方库,您应该在 UITableView 行中执行此操作。由于 UITableView 缓存单元格的方式,它在内存中是相对轻量级的。当然比 UIScrollView 中可能非常大的 UIView 更重要。这两种方式我都做到了,而且我对 UITableView 更满意。

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

          【讨论】:

            【解决方案5】:

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

            Apple Docs on CollectionViews

            另外,请参阅关于它们的两个 WWDC 2012 会议:

            Introduction to Collection Views
            Advanced Collection Views

            遗憾的是,Apple 没有包含简单的画廊或封面流布局,但制作起来很容易。

            【讨论】:

            • 没有。只有 6 个,但至少你应该指向那个方向。
            • 这是默认设置.. 文档实际上非常好,自从这篇文章以来,我有机会在项目中做架子等,还不错..
            【解决方案6】:

            我写了一篇关于使用 UICollectionView 构建媒体库的教程。它从用户的照片库中填充。我认为它非常适合您尝试做的事情。

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

            希望对您有所帮助。干杯!

            【讨论】:

            • 谢谢。尽管这个问题实际上是在两年前发布的,但您的帖子仍然很有用。顺便说一句,最好在这里发布完整的解决方案。
            【解决方案7】:

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

            首先我用 ImageView 制作了一个自定义 Collection View 单元格

            在 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 (sn-p) 中

            UICollectionView *collectionViewThumbnails;
            

            在 ThumbNailViewController.m (sn-p) 中

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

            希望能回答你的问题。

            【讨论】:

              【解决方案8】:

              这是一个非常好的库,名为 FGallery 用于 iOS

              -支持自动旋转

              -缩略图视图

              -缩放

              -删除

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-09-21
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多