实际上,我们可以使用李明杰在教程里集成的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 = ; 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 = ; i<count; i++) {
HMPhoto *pic = self.pic_urls[i]; MJPhoto *photo = [[MJPhoto alloc] init];
// 设置图片的路径
photo.url = [NSURL URLWithString:pic.bmiddle_pic];
// 设置来源于哪一个UIImageView
photo.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 = ; 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 = ; 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 - ) * StatusPhotoMargin; // 行数
NSUInteger rows = (count + maxCols - ) / maxCols;
CGFloat photosH = rows * StatusPhotoWH + (rows - ) * 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

iOS 新浪微博-5.3 首页微博列表_集成图片浏览器的更多相关文章

  1. iOS 新浪微博-5.1 首页微博列表_时间/配图

    在上一篇中,我们已经把首页微博显示出来了,但还有很多细节,需要我们去调整的.这一章中,我们将处理好时间,配图,工具框及转发微博等小细节的功能. 时间处理 第一步:定义一个时间的类别,用于判断是昨天.今 ...

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

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

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

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

  4. IOS SWIFT UITableView 实现简单微博列表

    // // Weibo.swift // UITableViewCellExample // // Created by XUYAN on 15/8/15. // Copyright (c) 2015 ...

  5. 新浪微博登陆以及发送微博(附python源码)

    原文链接(本人):https://blog.csdn.net/A5878989/article/details/76275855 说明 本文主要记录分析新浪微博登陆以及发送文字和图片微博的详细过程 分 ...

  6. Day8-微信小程序实战-交友小程序-首页用户列表渲染及多账号调试及其点赞功能的实现

    在这之前已经把编辑个人的所有信息的功能已经完成了 之后先对首页的列表搞动态的,之前都是写死的静态 1.之前都是把好友写死的,现在就在js里面定义一个数组,用循环来动态的绑定 在onReady中定义,取 ...

  7. 【Android 我的博客APP】1.抓取博客首页文章列表内容——网页数据抓取

    打算做个自己在博客园的博客APP,首先要能访问首页获取数据获取首页的文章列表,第一步抓取博客首页文章列表内容的功能已实现,在小米2S上的效果图如下: 思路是:通过编写的工具类访问网页,获取页面源代码, ...

  8. PHPCMS v9 实现首页,列表页,内容页调用点击量方法

    大家好,今天有点闲,看很多朋友经常问PHPCMS v9 首页,列表页,内容页调用点击怎么弄,打算抽时间把代码全部归纳出来,以便大家日后使用,如下: 1,首页调用点击量 {pc:content acti ...

  9. 织梦首页、列表页调用文章body内容的两种方法

    http://blog.csdn.net/langyu1021/article/details/52261411 关于首页.列表页调用文章body内容的两种方法,具体方法如下: 第一种方法: {ded ...

随机推荐

  1. 掌握业界最新工程实践 | 了解AIOps下一代微服务等最新趋势

    近年来,IT应用越来越复杂,一旦出现故障,诊断越来越困难,使用传统技术来管理机器数据的组织会让其运维团队不堪重负.幸好随着大数据.机器学习和AI技术的飞速发展,智能化运维给这一现状带来了改变. 那么就 ...

  2. html学习_表格、表单

    表格(table):是用来处理表格式数据的,不是用来布局的. table > tr(行标签)>  td(单元格标签) 1.表格注意事项: tr只能放置td标签,td里面可以放置任意元素. ...

  3. 手把手教你用Spring Cloud和Docker构建微服务

    什么是Spring Cloud? Spring Cloud 是Pivotal提供的用于简化分布式系统构建的工具集.Spring Cloud引入了云平台连接器(Cloud Connector)和服务连接 ...

  4. 为什么实数系里不存在最小正数?(Why the smallest positive real number doesn't exist in the real number system ?)

    We define the smallest positive real number as the number which is explicitly greater than zero and ...

  5. webstom 快捷键

  6. Rodrigues Formula

    https://en.wikipedia.org/wiki/Rodrigues%27_formula https://en.wikipedia.org/wiki/Rodrigues%27_rotati ...

  7. 垃圾回收GC3种算法的衍生品 增量回收:预测和控制GC所产生的中断时间

    小结: 1.GC和程序处理的本质是无关的: 2.增量回收:预测和控制GC所产生的中断时间: 1. 分代回收 GC和程序处理的本质是无关的,因此它所消耗的时间越短越好.分代回收的目的,正是为了在程序 运 ...

  8. canvas 线性规划

    小结: 1.线性规划 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> &l ...

  9. day5_集合

    集合也是一种数据类型,一个类似列表东西,它的特点是无序的,不重复的,也就是说集合中是没有重复的数据 集合的作用: 1.它可以把一个列表中重复的数据去掉,而不需要你再写判断---天生去重 2.可以做关系 ...

  10. 贪吃蛇java版

    主要的蛇的类 import java.awt.Color; import java.awt.Graphics; import java.awt.HeadlessException; import ja ...