MWPhotoBrowser-ModalPics 1.4.2

MWPhotoBrowser-ModalPics 1.4.2

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布最后发布2015年6月

Sergio García 维护。



 
依赖
SDWebImage~> 3.7
MBProgressHUD~> 0.8
DACircularProgress>= 0
PSTCollectionView~> 1.2
 


  • Sergio Garcia

MWPhotoBrowser

Flattr this git repo

一个简单的 iOS 图像浏览器,可选项包括网格视图、标题和选择。

MWPhotoBrowser 可以通过提供 UIImage 对象或文件的 URL 来显示一个或多个图片,可以是本地文件、网页图片或库资源。照片浏览器能够无缝地处理从网络下载和缓存图片。照片可以缩放和平移,并可显示可自定义的标题。

浏览器也可以用于允许用户通过网格或主图像视图选择一个或多个图片。

Alt    Alt    Alt    Alt    Alt    Alt

适用于 iOS 5.1.1 及以上版本。所有字符串均可本地化,因此可适用于支持多语言的应用。

使用

MWPhotoBrowser被设计为在导航控制器中展示。只需要设置代表(必须遵循MWPhotoBrowserDelegate)并实现2个必需的代表方法,以便以MWPhoto对象的形式为图片浏览器提供数据。您可以通过提供一个UIImage对象或包含文件路径、在线图片或从资产库中获取的资源的URL来创建一个MWPhoto对象。

MWPhoto对象为您处理缓存、文件管理、网络图像下载以及各种优化。如果您想使用自己的数据模型来表示图片,则可以简单确保您的模型遵循MWPhoto协议。然后您可以自己处理缓存、下载等管理。有关此信息的更多信息,请参阅MWPhotoProtocol.h

请查看下面的代码片段,了解如何实现图片浏览器。项目中也包含了一个简单的演示应用。

// Create array of MWPhoto objects
self.photos = [NSMutableArray array];
[photos addObject:[MWPhoto photoWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"photo2l" ofType:@"jpg"]]]];
[photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3629/3339128908_7aecabc34b.jpg"]]];
[photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3590/3329114220_5fbc5bc92b.jpg"]]];

// Create browser (must be done each time photo browser is
// displayed. Photo browser objects cannot be re-used)
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];

// Set options
browser.displayActionButton = YES; // Show action button to allow sharing, copying, etc (defaults to YES)
browser.displayNavArrows = NO; // Whether to display left and right nav arrows on toolbar (defaults to NO)
browser.displaySelectionButtons = NO; // Whether selection buttons are shown on each image (defaults to NO)
browser.zoomPhotosToFill = YES; // Images that almost fill the screen will be initially zoomed to fill (defaults to YES)
browser.alwaysShowControls = NO; // Allows to control whether the bars and controls are always visible or whether they fade away to show the photo full (defaults to NO)
browser.enableGrid = YES; // Whether to allow the viewing of all the photo thumbnails on a grid (defaults to YES)
browser.startOnGrid = NO; // Whether to start on the grid of thumbnails instead of the first photo (defaults to NO)
browser.wantsFullScreenLayout = YES; // iOS 5 & 6 only: Decide if you want the photo browser full screen, i.e. whether the status bar is affected (defaults to YES)

// Optionally set the current visible photo before displaying
[browser setCurrentPhotoIndex:1];

// Present
[self.navigationController pushViewController:browser animated:YES];

// Manipulate
[browser showNextPhotoAnimated:YES];
[browser showPreviousPhotoAnimated:YES];
[browser setCurrentPhotoIndex:10];

然后响应所需的代表方法

- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return self.photos.count;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < self.photos.count)
        return [self.photos objectAtIndex:index];
    return nil;
}

您可以通过将其包裹在一个新的导航控制器中并展示来模态地展示浏览器。演示应用允许您在两种展示类型之间切换。

如果您使用iOS 5或6,并且不想全屏查看图片浏览器(例如,如果您正在使用视图控制器包含),则将图片浏览器的wantsFullScreenLayout属性设置为NO。这表示状态栏将不受图片浏览器的影响。

网格

为了正确展示缩略图网格,您必须确保将属性enableGrid设置为YES,并实现以下代表方法

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index;

通过启用startOnGrid属性,图片浏览器还可以从网格开始。

操作

默认情况下,如果操作按钮可见,则图像(以及如果存在,则标题)会发送到UIActivityViewController。在iOS 5中,会出现一个自定义操作表,允许它们复制或通过电子邮件发送图片。

您可以通过实现以下代表方法提供自定义操作

- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser actionButtonPressedForPhotoAtIndex:(NSUInteger)index {
    // Do your thing!
}

图片标题

通过在特定的图片上设置caption属性,可以简单地显示图片标题

MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3629/3339128908_7aecabc34b.jpg"]];
photo.caption = @"Campervan";

如果不设置标题属性,则不会显示标题。

自定义标题

默认情况下,标题是一个简单的黑色透明视图,标签显示图片的标题为白色。如果您想实现自己的标题视图,请按照以下步骤操作

  1. 可选地,使用MWPhoto的子类来存储您的图片,以便您可以存储比简单的标题字符串更多的数据。
  2. 子类MWCaptionView并重写-setupCaption-sizeThatFits:(以及任何其他您觉得合适的UIView方法)来自定义视图布局并设置其大小。更多信息请查阅MWCaptionView.h
  3. 实现-photoBrowser:captionViewForPhotoAtIndex: MWPhotoBrowser代表方法(如下所示)。

自定义标题视图的代表方法示例

- (MWCaptionView *)photoBrowser:(MWPhotoBrowser *)photoBrowser captionViewForPhotoAtIndex:(NSUInteger)index {
    MWPhoto *photo = [self.photos objectAtIndex:index];
    MyMWCaptionViewSubclass *captionView = [[MyMWCaptionViewSubclass alloc] initWithPhoto:photo];
    return captionView;
}

选择

图片浏览器可以显示复选框,允许用户选择一个或多个图片。要使用此功能,只需启用displaySelectionButtons属性,并实现以下代表方法

- (BOOL)photoBrowser:(MWPhotoBrowser *)photoBrowser isPhotoSelectedAtIndex:(NSUInteger)index {
    return [[_selections objectAtIndex:index] boolValue];
}

- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index selectedChanged:(BOOL)selected {
    [_selections replaceObjectAtIndex:index withObject:[NSNumber numberWithBool:selected]];
}

添加到你的项目中

方法 2:静态库

  1. 从GitHub获取最新源代码,可以选择下载为zip文件,或者在git://github.com/mwaterfall/MWPhotoBrowser.git上克隆仓库,并将代码存储在您希望的地方。
  2. 在导航器中右键单击您的项目,点击“将文件添加到‘您的项目’”,然后浏览并选择“MWPhotoBrowser.xcodeproj”。
  3. 在项目目标设置中,转到“构建阶段” -> “与二进制链接的库”并添加libMWPhotoBrowser.a
  4. 仍然在“构建阶段”中,下拉“复制资源包”,并将文件MWPhotoBrowser.bundle从MWPhotoBrowser项目拖放到该列表中。这确保了您的项目将包含照片浏览器正常工作所需的图形。
  5. 在目标中,选择“构建设置”选项卡,确保“始终搜索用户路径”设置为YES,并且“用户头文件搜索路径”设置为递归的绝对或相对路径,该路径指向存储MWPhotoBrowser代码的目录。在MWPhotoBrowser项目的文件布局中,简单的../**可以作为示例项目文件夹和MWPhotoBrowser项目文件夹相邻的示例。如果您在这方面遇到任何问题,请告知我。
  6. 在“构建阶段 / 与二进制链接的库”下添加MessageUI.frameworkQuartzCore.frameworkAssetsLibrary.frameworkImageIO.framework到“链接的框架和库”。

现在您应该可以将MWPhotoBrowser.h添加到您的项目中,并开始使用它。

在Xcode中设置这些项目可能会有些棘手,所以如果您遇到任何问题,可能会希望阅读一些信息

方法3:直接将源代码包含在项目中

另一种方法是简单地添加文件到您的Xcode项目,如果需要,将它们复制到项目目录中。确保包括MWPhotoBrowser/ClassesMWPhotoBrowser/LibrariesMWPhotoBrowser.bundle中的所有代码。

备注和认可

MWPhotoBrowser非常感激地使用了这些其他出色的开源项目

  • SDWebImage由Olivier Poitrey — 用于处理下载和从网络中解压照片。
  • MBProgressHUD由Jonathan George — 用于显示活动通知。
  • DACircularProgress由Daniel Amitay — 用于显示图像下载进度。

示例照片由Oliver Waters提供(http://twitter.com/oliverwaters)。

许可证

版权所有(c)2010 Michael Waterfall [email protected]

任何人,免费取得此软件及其相关文档文件(“软件”)的副本(以下简称“软件”),可以在不受到限制的情况下处理软件,包括但不限于使用、复制、修改、合并、出版、分发、再许可和/或出售软件的副本,并允许向软件提供的人这样做,前提是以下条件

上述版权声明和本许可声明应包括在软件的所有副本或主要部分中。

本软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定目的的适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论是否因合同、侵权或其他法律行为,以及与软件或软件的使用或其他相关行为有关。