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 <SDWebImage/UIImageView+WebCache.h>
@interface ViewController ()<YBImageBrowserDataSource>{
    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.效果

                      

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
使用 MJPhotoBrowser 框架实现图片浏览器的步骤如下: 1. 首先需要使用 CocoaPods 将 MJPhotoBrowser 框架导入到项目中。 2. 导入框架头文件:#import "MJPhotoBrowser.h" 3. 在需要显示图片浏览器的地方,创建一个数组用来存放图片的 URL 或 UIImage 对象。 4. 遍历图片数组,将每张图片转换成 MJPhoto 对象,并将其添加到 MJPhotoBrowser 控制器的 photoArray 属性中。 5. 创建 MJPhotoBrowser 控制器对象,并设置其 currentPhotoIndex 属性为当前图片的下标。 6. 调用 presentViewController:animated:completion: 方法,将 MJPhotoBrowser 控制器推出来展示图片浏览器。 以下是示例代码: ```objc // 创建图片数组 NSMutableArray *photos = [NSMutableArray array]; for (int i = 0; i < self.imageArray.count; i++) { // 创建 MJPhoto 对象 MJPhoto *photo = [[MJPhoto alloc] init]; // 设置图片的 URL 或 UIImage 对象 photo.url = [NSURL URLWithString:self.imageArray[i]]; // 设置图片所对应的原始 UIImageView photo.srcImageView = self.imageViewArray[i]; // 添加到图片数组中 [photos addObject:photo]; } // 创建 MJPhotoBrowser 控制器对象 MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init]; // 设置图片数组 browser.photos = photos; // 设置当前显示的图片下标 browser.currentPhotoIndex = index; // 显示图片浏览器 [self presentViewController:browser animated:YES completion:nil]; ``` 其中,self.imageArray 和 self.imageViewArray 分别为存放图片 URL 或 UIImageView 对象的数组,index 为当前需要显示的图片下标。在上述代码中,我们将图片 URL 和对应的 UIImageView 对象一起存放到 MJPhoto 对象中,这样在图片浏览器中浏览时,就可以自动放大到对应的 UIImageView 的位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值