测试已测试 | ✗ |
Lang语言 | Obj-CObjective C |
许可证 | MIT |
发布最后发布 | 2015年6月 |
由 Sergio García 维护。
依赖 | |
SDWebImage | ~> 3.7 |
MBProgressHUD | ~> 0.8 |
DACircularProgress | >= 0 |
PSTCollectionView | ~> 1.2 |
MWPhotoBrowser 可以通过提供 UIImage
对象或文件的 URL 来显示一个或多个图片,可以是本地文件、网页图片或库资源。照片浏览器能够无缝地处理从网络下载和缓存图片。照片可以缩放和平移,并可显示可自定义的标题。
浏览器也可以用于允许用户通过网格或主图像视图选择一个或多个图片。
适用于 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";
如果不设置标题属性,则不会显示标题。
默认情况下,标题是一个简单的黑色透明视图,标签显示图片的标题为白色。如果您想实现自己的标题视图,请按照以下步骤操作
MWPhoto
的子类来存储您的图片,以便您可以存储比简单的标题字符串更多的数据。MWCaptionView
并重写-setupCaption
和-sizeThatFits:
(以及任何其他您觉得合适的UIView方法)来自定义视图布局并设置其大小。更多信息请查阅MWCaptionView.h
-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]];
}
git://github.com/mwaterfall/MWPhotoBrowser.git
上克隆仓库,并将代码存储在您希望的地方。libMWPhotoBrowser.a
。MWPhotoBrowser.bundle
从MWPhotoBrowser项目拖放到该列表中。这确保了您的项目将包含照片浏览器正常工作所需的图形。MessageUI.framework
、QuartzCore.framework
、AssetsLibrary.framework
和ImageIO.framework
到“链接的框架和库”。现在您应该可以将MWPhotoBrowser.h
添加到您的项目中,并开始使用它。
在Xcode中设置这些项目可能会有些棘手,所以如果您遇到任何问题,可能会希望阅读一些信息
另一种方法是简单地添加文件到您的Xcode项目,如果需要,将它们复制到项目目录中。确保包括MWPhotoBrowser/Classes
、MWPhotoBrowser/Libraries
和MWPhotoBrowser.bundle
中的所有代码。
MWPhotoBrowser非常感激地使用了这些其他出色的开源项目
示例照片由Oliver Waters提供(http://twitter.com/oliverwaters)。
版权所有(c)2010 Michael Waterfall [email protected]
任何人,免费取得此软件及其相关文档文件(“软件”)的副本(以下简称“软件”),可以在不受到限制的情况下处理软件,包括但不限于使用、复制、修改、合并、出版、分发、再许可和/或出售软件的副本,并允许向软件提供的人这样做,前提是以下条件
上述版权声明和本许可声明应包括在软件的所有副本或主要部分中。
本软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定目的的适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论是否因合同、侵权或其他法律行为,以及与软件或软件的使用或其他相关行为有关。