实际上,我们可以使用李明杰在教程里集成的MJPhotoBrowser,地址:

http://code4app.com/ios/快速集成图片浏览器/525e06116803fa7b0a000001

使用起来也很简单,只需要两步:

引入头文件:

#import "MJPhotoBrowser.h"
#import "MJPhoto.h"

给图片添加手势监听器及显示

- (id)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self) {// 预先创建9个图片控件for (int i = 0; i<HMStatusPhotosMaxCount; i++) {HMStatusPhotoView *photoView = [[HMStatusPhotoView alloc] init];photoView.tag = i;[self addSubview:photoView];// 添加手势监听器(一个手势监听器 只能 监听对应的一个view)UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] init];[recognizer addTarget:self action:@selector(tapPhoto:)];[photoView addGestureRecognizer:recognizer];}}return self;
}/***  监听图片的点击*/
- (void)tapPhoto:(UITapGestureRecognizer *)recognizer
{// 1.创建图片浏览器MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];// 2.设置图片浏览器显示的所有图片NSMutableArray *photos = [NSMutableArray array];int count = self.pic_urls.count;for (int i = 0; i<count; i++) {HMPhoto *pic = self.pic_urls[i];MJPhoto *photo = [[MJPhoto alloc] init];// 设置图片的路径photo.url = [NSURL URLWithString:pic.bmiddle_pic];// 设置来源于哪一个UIImageViewphoto.srcImageView = self.subviews[i];[photos addObject:photo];}browser.photos = photos;// 3.设置默认显示的图片索引browser.currentPhotoIndex = recognizer.view.tag;// 3.显示浏览器
    [browser show];
}

但这个库是2013年的,现在已经停止更新了,当然存在很多BUG.因为,这个项目中,选用另一个集成图片浏览器SDPhotoBrowser,细看了一下,应该是基于李明杰的修改的。

code4app : http://code4app.com/ios/SDPhotoBrowser/54db1e3f933bf0d44f8b5464

github : https://github.com/gsdios/SDPhotoBrowser

引用到项目中,使用方法和MJPhotoBrowser差不多。

头部引用

#import "SDPhotoBrowser.h"

给每个UIImageView设置手势

        photoView.tag = i;// 添加手势监听器(一个手势监听器 只能 监听对应的一个view)UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] init];[recognizer addTarget:self action:@selector(tapPhoto:)];[photoView addGestureRecognizer:recognizer];

实现相关的代码:

- (void)tapPhoto:(UITapGestureRecognizer *)recognizer
{SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];browser.sourceImagesContainerView = self; // 原图的父控件browser.imageCount = self.photos.count; // 图片总数browser.currentImageIndex = recognizer.view.tag;browser.delegate =self; //self.subviews[recognizer.view.tag];
    [browser show];
}#pragma mark - photobrowser代理方法// 返回临时占位图片(即原来的小图)
- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{return  [(StatusPhotoView *)self.subviews[index] image];
}// 返回高质量图片的url
- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{NSString *urlStr = [[self.photos[index] thumbnail_pic] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];return [NSURL URLWithString:urlStr];
}

完整的代码结构

//
//  StatusPhotosView.m
//  Weibo
//
//  Created by jiangys on 15/10/25.
//  Copyright © 2015年 Jiangys. All rights reserved.
//

#import "StatusPhotosView.h"
#import "StatusPhotoView.h"
#import "Photo.h"
#import "SDPhotoBrowser.h"#define StatusPhotoWH 70
#define StatusPhotoMargin 10
#define StatusPhotoMaxCol(count) ((count==4)?2:3)@implementation StatusPhotosView- (void)setPhotos:(NSArray *)photos
{_photos = photos;NSUInteger photosCount = photos.count;// 创建足够多的图片控制while (self.subviews.count < photosCount) {StatusPhotoView *photoView = [[StatusPhotoView alloc] init];[self addSubview:photoView];}// 遍历所有的图片控件,设置图片for (int i = 0; i < self.subviews.count; i++) {StatusPhotoView *photoView = self.subviews[i];if (i < photosCount) {photoView.photo = photos[i];photoView.hidden = NO;} else{photoView.hidden=YES;}photoView.tag = i;// 添加手势监听器(一个手势监听器 只能 监听对应的一个view)UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] init];[recognizer addTarget:self action:@selector(tapPhoto:)];[photoView addGestureRecognizer:recognizer];}}- (void)tapPhoto:(UITapGestureRecognizer *)recognizer
{SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];browser.sourceImagesContainerView = self; // 原图的父控件browser.imageCount = self.photos.count; // 图片总数browser.currentImageIndex = recognizer.view.tag;browser.delegate =self; //self.subviews[recognizer.view.tag];
    [browser show];
}#pragma mark - photobrowser代理方法// 返回临时占位图片(即原来的小图)
- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{return  [(StatusPhotoView *)self.subviews[index] image];
}// 返回高质量图片的url
- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{NSString *urlStr = [[self.photos[index] thumbnail_pic] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];return [NSURL URLWithString:urlStr];
}- (void)layoutSubviews
{[super layoutSubviews];// 设置图片的尺寸和位置NSUInteger photosCount = self.photos.count;int maxCol = StatusPhotoMaxCol(photosCount);for (int i = 0; i<photosCount; i++) {StatusPhotoView *photoView = self.subviews[i];int col = i % maxCol;photoView.x = col * (StatusPhotoWH + StatusPhotoMargin);int row = i / maxCol;photoView.y = row * (StatusPhotoWH + StatusPhotoMargin);photoView.width = StatusPhotoWH;photoView.height = StatusPhotoWH;}
}+ (CGSize)sizeWithCount:(NSUInteger)count
{// 最大列数(一行最多有多少列)int maxCols = StatusPhotoMaxCol(count);NSUInteger cols = (count >= maxCols)? maxCols : count;CGFloat photosW = cols * StatusPhotoWH + (cols - 1) * StatusPhotoMargin;// 行数NSUInteger rows = (count + maxCols - 1) / maxCols;CGFloat photosH = rows * StatusPhotoWH + (rows - 1) * StatusPhotoMargin;return CGSizeMake(photosW, photosH);
}
@end

要注意,因为是UIImageView,想点击某个图片能交互,需要给UIImageView开启交互功能。

StatusPhotoView.m -- >initWithFrame

     // 开启交互self.userInteractionEnabled = YES;

最终效果:

章节源代码下载:http://pan.baidu.com/s/1qWtKrMG

新浪微博Github:https://github.com/jiangys/Weibo

转载于:https://www.cnblogs.com/jys509/p/4966811.html

iOS 新浪微博-5.3 首页微博列表_集成图片浏览器相关推荐

  1. iOS 新浪微博-5.2 首页微博列表_转发微博/工具栏

    继续于上一篇,还是做首页的功能,这一篇把剩下的首页继续完善. 看看上面的图片,分析: 1.转发微博里面的内容,和原创微博是一样的,由文字+配图组成.这应该放在一个UIView里处理. 2.工具栏也当成 ...

  2. iOS 新浪微博-5.0 首页微博列表

    首页显示微博列表,是微博的核心部分,这一章节,我们主要是显示出微博的列表. 导入第三方类库 pod 'SDWebImage', '~> 3.7.3' pod 'MJRefresh', '~> ...

  3. ios 高德地图加载瓦片地图_集成iOS高德地图

    一.前奏 这里只是个人集成过程中的遇到的点,现做下标记. 1,如果你需要集成涉及国外版地图,基于HTTPS的最新版本需要设置,需要注意基于国外定位,高德的不太精确(个人测试) [self.mapVie ...

  4. newduba首页怎么去掉_解决Chrome浏览器主页被毒霸劫持/篡改

    一觉醒来,发现Chrome的主页被篡改了,无论设置什么主页,都会跳转到毒霸的导航网站. 看着浓浓的山寨hao123风味的页面,我不禁陷入了沉思.没想到这么多年过去了,做个页面连hao123都不如. 此 ...

  5. JS实现仿新浪微博大厅和腾讯微博首页滚动效果_前端开发

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  6. 新浪微博客户端开发之授权登录+获取微博列表

    新浪微博客户端开发之授权登录+获取微博列表 闲篇: 最近实在是乱得不行,至于怎么乱我也不知该怎么说,那么久没发博客就证明了这点,一般如果小巫有做详尽的计划,并把时间投入到上面的话,我是可以用最短的时间 ...

  7. scrapy 爬取新浪微博 的微博列表及微博内容

    代码地址:GitHub 参考:博客 通过scrapy框架爬取指定账号的信息和微博 截止到目前(2019年01月15日)的微博账号粉丝排名: 爬取方法:提取网页版的微博接口 1.重写start_requ ...

  8. iOS 类似简单的第三方微博客户端,可进行登录微博浏览相关信息

    Demo地址:https://github.com/ChenNan-FRAM/Fenvo (如果你觉得有用麻烦star一下感激不尽) Fenvo Objective-C, iOS 类似简单的第三方微博 ...

  9. 『TensorFlow』函数查询列表_张量属性调整

    博客园 首页 新随笔 新文章 联系 订阅 管理 『TensorFlow』函数查询列表_张量属性调整 数据类型转换Casting 操作 描述 tf.string_to_number (string_te ...

  10. iOS 商城类 app 首页的实现

    2019独角兽企业重金招聘Python工程师标准>>> iOS 商城类 app 首页的实现 很多人做 iOS开发的人员都应该写过这样的界面,但是呢,具体怎么去优化写这样的界面是我们需 ...

最新文章

  1. 轻量级WEB开发框架flask
  2. 基于springboot实现疫情数据统计系统
  3. [唐诗]入朝洛堤步月-上官仪
  4. esp8266 wifi模组手机一键配网,配置一次,下次重启设备后不需再进行配网
  5. 听说你,对薪酬待遇不太满意。。。
  6. 配置错误定义了重复的“system.web.extensions/scripting/scriptResourceHandler” 解决办法...
  7. oauth2.0里回调地址返回code中如何让code不显示在URL里?
  8. c语言程序装萝卜,萝卜花园练习win7系统安装SkyDrive的图文步骤
  9. Hive 复制分区表和数据
  10. 手机配置都赶上笔记本了
  11. 有什么好用的画c语言流程图的软件?
  12. 新版TP开发小额贷系统源码+可封装IOS安卓双端
  13. ArcGIS空间数据的拓扑处理
  14. GitLab之docker注册Runner
  15. android进入recovery模式,Android关机重启至recovery安卓进入Recovery模式模式
  16. 贴吧怎么发帖,发防删图出现审核怎么办?
  17. 知识图谱的相关技术-概览(笔记)
  18. 日本风俗业数据_神奇宝贝如何融入日本民俗
  19. MEC与C-V2X融合应用场景白皮书
  20. JavaWeb - 小米商城 :首页商品分类展示

热门文章

  1. machine learning 之 Neural Network 3
  2. Repeater绑定数据库
  3. 理解Servlet及其对象
  4. DateTable复制表行
  5. linux自建git仓库
  6. umount强制卸载不起作用,卸载光驱终极办法---fuser
  7. iOS学习笔记总结整理
  8. Dubbo集成Spring与Zookeeper实例
  9. 栈的应用1——超级计算器(中缀与后缀表达式)C语言
  10. 5.编译并运行erlang程序