MWPhotoBrowser 是一个强大且古老的图片浏览库,在GitHub上有英文版的详细使用说明。它同时依赖DACircularProgress ,MBProgressHUD ,SDWebImage。
GitHub地址
所以很多人建议是用cocoaPods导入 ,但如果导入后它所依赖的其它三个库是低版本的,例如MBProgressHUD是0.92的,想用1.0版本时,再导入一次MBProgressHUD 1.0很容易发生冲突出错,暂未解。
@interface ViewController ()<MWPhotoBrowserDelegate>
@property (nonatomic,retain) NSMutableArray *photosArray;
@property (nonatomic,retain) NSMutableArray *thumbArray;
@property (nonatomic,retain) NSMutableArray *selectedArray;
@end
- (void)initPhotos{
//先清空数组
[self.photosArray removeAllObjects];
[self.thumbArray removeAllObjects];
//再添加图片
for (int i = 0;i < 5; i++) {
MWPhoto *photo = [MWPhoto photoWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]]];
photo.caption = [NSString stringWithFormat:@"第%d张图片",i];
[self.photosArray addObject:photo];
}
//添加照片
//MWPhoto *photo1 = [MWPhoto photoWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"0" ofType:@"jpg"]]];
//MWPhoto *photo2 = [MWPhoto photoWithURL:[NSURL URLWithString:@"http://image.tianjimedia.com/uploadImages/2014/113/40/PMCZD10TC4UM_680x500.jpg"]];
//添加视频
MWPhoto *video = [MWPhoto photoWithURL:[NSURL URLWithString:@"https://scontent.cdninstagram.com/hphotos-xpt1/t51.2885-15/e15/11192696_824079697688618_1761661_n.jpg"]];
video.videoURL = [[NSURL alloc] initWithString:@"https://scontent.cdninstagram.com/hphotos-xpa1/t50.2886-16/11200303_1440130956287424_1714699187_n.mp4"];
video.caption = @"这是一个视频";
[self.photosArray addObject:video];
self.thumbArray = self.photosArray;
//初始化
MWPhotoBrowser *photoBrowser = [[MWPhotoBrowser alloc]initWithDelegate:self];
//set options
[photoBrowser setCurrentPhotoIndex:0];
photoBrowser.displayActionButton = YES;//显示分享按钮(左右划动按钮显示才有效)
photoBrowser.displayNavArrows = YES; //显示左右划动
photoBrowser.displaySelectionButtons = YES; //是否显示选择图片按钮
photoBrowser.alwaysShowControls = NO; //控制条始终显示
photoBrowser.zoomPhotosToFill = YES; //是否自适应大小
photoBrowser.enableGrid = YES;//是否允许网络查看图片
photoBrowser.startOnGrid = YES; //是否以网格开始;
photoBrowser.enableSwipeToDismiss = YES;
photoBrowser.autoPlayOnAppear = NO;//是否自动播放视频
//这样处理的目的是让整个页面跳转更加自然
UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:photoBrowser];
navC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:navC animated:YES completion:nil];
}
接下来主要就是MWPhotoBrowser的代理方法
#pragma mark - MWPhotosBrowserDelegate
//必须实现的方法
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser{
return self.photosArray.count;
}
- (id<MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index{
if (index < self.photosArray.count) {
return [self.photosArray objectAtIndex:index];
}
return nil;
}
//可选方法
- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser didDisplayPhotoAtIndex:(NSUInteger)index{
NSLog(@"当前显示图片编号----%ld",index);
}
- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser actionButtonPressedForPhotoAtIndex:(NSUInteger)index{
NSLog(@"分享按钮的点击方法----%ld",index);
}
- (BOOL)photoBrowser:(MWPhotoBrowser *)photoBrowser isPhotoSelectedAtIndex:(NSUInteger)index{
//浏览图片时是图片是否选中状态
return NO;
}
- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index selectedChanged:(BOOL)selected{
//selected表示是否选中
if (selected) {
[self.selectedArray addObject:@(index)];
NSLog(@"第%ld张图片在被选中",index);
}else{
[self.selectedArray removeObject:@(index)];
NSLog(@"第%ld张图片在被选中",index);
}
}
//有navigationBar时title才会显示
- (NSString *)photoBrowser:(MWPhotoBrowser *)photoBrowser titleForPhotoAtIndex:(NSUInteger)index{
NSString *str = nil;
switch (index) {
case 0 :
str = @"这是第111张图片";
break;
case 1 :
str = @"这是第222张图片";
break;
case 2 :
str = @"这是第333张图片";
break;
default:
break;
}
return str
;
}
//如果要看缩略图必须实现这个方法
- (id<MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index{
return [self.thumbArray objectAtIndex:index];
}
-(void)photoBrowserDidFinishModalPresentation:(MWPhotoBrowser *)photoBrowser{
[self dismissViewControllerAnimated:YES completion:nil];
}
PS:
- MWPhotoBrowser 更多的是做为图片的浏览更合适,而不太适合做为图片上传的选择框架,选择的话QBImagePickerController更合适。
- 如果不是特定图片而是要浏览相册中的图片的话,由于涉及到8.0前后苹果对相册的调整,方法都不相同,所以要做各种版本和权限的判断。
网友评论