IOS-图片浏览之YBImageBrowser的简单使用

1.安装

第一种方式 使用 cocoapods

pod 'YBImageBrowser'    

注意:请尽量使用最新版本(1.1.2);若搜索不到库,可使用rm ~/Library/Caches/CocoaPods/search_index.json移除本地索引然后再执行安装,或者更新一下 cocoapods 版本。

第二种方式 手动导入

直接将该 Demo 的 YBImageBrowser 文件夹拖入你的工程中,并在你的 Podfile 里面添加:

pod 'SDWebImage', '~> 4.3.3'
pod 'FLAnimatedImage', '~> 1.0.12'


2.使用

我这里是采用代理数据源的方式,完整代码如下:

#import "ViewController.h"
#import "YBImageBrowser.h"
#import
@interface ViewController (){
NSArray *imageArray;
NSMutableArray *imageViewArray;
NSInteger currentIndex;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
imageViewArray = [[NSMutableArray alloc]init];
imageArray = @[
@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524118687954&di=d92e4024fe4c2e4379cce3d3771ae105&imgtype=0&src=http%3A%2F%2Fimg3.duitang.com%2Fuploads%2Fitem%2F201605%2F18%2F20160518181939_nCZWu.gif",
@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524118772581&di=29b994a8fcaaf72498454e6d207bc29a&imgtype=0&src=http%3A%2F%2Fimglf2.ph.126.net%2F_s_WfySuHWpGNA10-LrKEQ%3D%3D%2F1616792266326335483.gif",
@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524118803027&di=beab81af52d767ebf74b03610508eb36&imgtype=0&src=http%3A%2F%2Fe.hiphotos.baidu.com%2Fbaike%2Fpic%2Fitem%2F2e2eb9389b504fc2995aaaa1efdde71190ef6d08.jpg",
@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524118823131&di=aa588a997ac0599df4e87ae39ebc7406&imgtype=0&src=http%3A%2F%2Fimg3.duitang.com%2Fuploads%2Fitem%2F201605%2F08%2F20160508154653_AQavc.png",
@"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=722693321,3238602439&fm=27&gp=0.jpg",
@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524118892596&di=5e8f287b5c62ca0c813a548246faf148&imgtype=0&src=http%3A%2F%2Fwx1.sinaimg.cn%2Fcrop.0.0.1080.606.1000%2F8d7ad99bly1fcte4d1a8kj20u00u0gnb.jpg",
@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524118914981&di=7fa3504d8767ab709c4fb519ad67cf09&imgtype=0&src=http%3A%2F%2Fimg5.duitang.com%2Fuploads%2Fitem%2F201410%2F05%2F20141005221124_awAhx.jpeg",
@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524118934390&di=fbb86678336593d38c78878bc33d90c3&imgtype=0&src=http%3A%2F%2Fi2.hdslb.com%2Fbfs%2Farchive%2Fe90aa49ddb2fa345fa588cf098baf7b3d0e27553.jpg",
@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524118984884&di=7c73ddf9d321ef94a19567337628580b&imgtype=0&src=http%3A%2F%2Fimg5q.duitang.com%2Fuploads%2Fitem%2F201506%2F07%2F20150607185100_XQvYT.jpeg"
];
[self initUI];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)initUI{
NSInteger rowCount = 3;
CGFloat width = self.view.bounds.size.width;
CGFloat imgW = width/rowCount;
CGFloat imgH = imgW;
CGFloat xPoint = 0;
CGFloat yPoint = 100;
NSInteger index = 0;
for (NSString *imgUrl in imageArray) {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(xPoint, yPoint, imgW, imgH)];
button.userInteractionEnabled = YES;
button.tag = index;
//点击图片放大
[button addTarget:self action:@selector(imgViewClick:) forControlEvents:UIControlEventTouchUpInside];
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, imgW, imgH)];
[button addSubview:img];

[img sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"no_img.png"]];
[imageViewArray addObject:img];

xPoint += imgW;
if ((index+1)%rowCount==0) {
yPoint += imgH;
xPoint = 0;
}
[self.view addSubview:button];
index++;
}
}
-(void)imgViewClick:(UIButton *)btn{
currentIndex = btn.tag;
YBImageBrowser *browser = [YBImageBrowser new];
browser.dataSource = self;
browser.currentIndex = btn.tag;
//展示
[browser show];
}
//YBImageBrowserDataSource 代理实现赋值数据
- (NSInteger)numberInYBImageBrowser:(YBImageBrowser *)imageBrowser {
return imageArray.count;
}
- (YBImageBrowserModel *)yBImageBrowser:(YBImageBrowser *)imageBrowser modelForCellAtIndex:(NSInteger)index {
NSString *urlStr = [imageArray objectAtIndex:index];
YBImageBrowserModel *model = [YBImageBrowserModel new];
model.url = [NSURL URLWithString:urlStr];
//model.sourceImageView = [imageViewArray objectAtIndex:index];
return model;
}
- (UIImageView *)imageViewOfTouchForImageBrowser:(YBImageBrowser *)imageBrowser {
return [imageViewArray objectAtIndex:currentIndex];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end


3.效果


git地址:https://github.com/indulgeIn/YBImageBrowser


0 个评论

要回复文章请先登录注册