使用串行线程实现图片瀑布流加载

2024-06-07 21:18

本文主要是介绍使用串行线程实现图片瀑布流加载,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

.h文件

typedef NS_ENUM(NSInteger, LowerList) {Left = 1,Right = 2
};@interface VC_AddStoryModel : UIViewController
{UIScrollView *scroll;LowerList lower;CGFloat viewsWidth;UIView *leftView;CGFloat leftHeight;UIView *rightView;CGFloat rightHeight;BOOL firstPicture;
}

.h文件

- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.self.view.backgroundColor = [UIColor whiteColor];{viewsWidth = ScreenWidth / 2;firstPicture = YES;scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64)];scroll.contentSize = CGSizeMake(ScreenWidth, ScreenHeight);scroll.backgroundColor = [UIColor whiteColor];[self.view addSubview:scroll];leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewsWidth, 0)];rightView = [[UIView alloc]initWithFrame:CGRectMake(SCREENWIDTH / 2, 0, viewsWidth, 0)];[scroll addSubview:leftView];[scroll addSubview:rightView];scroll.backgroundColor = [UIColor yellowColor];}[self layoutImage];}
- (void)layoutImage {NSArray* urls = @[@"http://img0.bdstatic.com/img/image/shouye/leimu/mingxing.jpg",@"http://img1.bdstatic.com/img/image/8662934349b033b5bb5c55e5d9834d3d539b700bcce.jpg",@"http://img.baidu.com/img/image/3bf33a87e950352a5947ae485143fbf2b2.jpg",@"http://imgstatic.baidu.com/img/image/7af40ad162d9f2d3cdc19be8abec8a136227cce1.jpg",@"http://imgstatic.baidu.com/img/image/weimeiyijing0207.jpg",@"http://e.hiphotos.baidu.com/image/w%3D400/sign=2e56c8010ed79123e0e095749d355917/ae51f3deb48f8c5470385d2638292df5e1fe7fd4.jpg",@"http://c.hiphotos.baidu.com/image/w%3D400/sign=e37cc47c6509c93d07f20ff7af3cf8bb/7a899e510fb30f2468cc6271ca95d143ad4b0369.jpg",@"http://b.hiphotos.baidu.com/image/w%3D400/sign=ac0b8e2b92ef76c6d0d2fa2bad17fdf6/a71ea8d3fd1f4134dedc5974271f95cad0c85ed4.jpg",@"http://imgstatic.baidu.com/img/image/huacaozhiwu0207.jpg",@"http://d.hiphotos.baidu.com/image/w%3D400/sign=7d27c75af4246b607b0eb374dbf81a35/5882b2b7d0a20cf4f28367d674094b36acaf99ac.jpg",];dispatch_queue_t queue = dispatch_queue_create("get image", DISPATCH_QUEUE_SERIAL);for (int i = 0; i < 100; i++) {__block  UIImage *image;@synchronized(scroll) {dispatch_async(queue, ^{NSString *imageUrl = urls[i % 10];NSURL *url = [NSURL URLWithString:imageUrl];NSData *data = [NSData dataWithContentsOfURL:url];image = [UIImage imageWithData:data];});dispatch_async(queue, ^{dispatch_async(dispatch_get_main_queue(), ^{[self makePictuerVisible:image];});});}}
}- (void)makePictuerVisible:(UIImage *)image {lower = [self checkList];CGFloat width = viewsWidth;CGFloat height = width * image.size.height / image.size.width;UIImageView *picture = nil;switch (lower) {case Left:{picture = [[UIImageView alloc]initWithFrame:CGRectMake(0, leftView.frame.size.height, width, height)];leftView.frame = CGRectMake(leftView.frame.origin.x, leftView.frame.origin.y, viewsWidth, leftView.frame.size.height + height);[leftView addSubview:picture];}break;case Right:picture = [[UIImageView alloc]initWithFrame:CGRectMake(0, rightView.frame.size.height, width, height)];rightView.frame = CGRectMake(rightView.frame.origin.x, rightView.frame.origin.y, viewsWidth, rightView.frame.size.height + height);[rightView addSubview:picture];break;default:break;}picture.image = image;if (Left == [self checkList]) {[scroll setContentSize:CGSizeMake(ScreenWidth, rightView.frame.size.height + 1)];} else {[scroll setContentSize:CGSizeMake(ScreenWidth, leftView.frame.size.height + 1)];}NSLog(@"%f   %f",scroll.contentSize.height, scroll.contentSize.width);
}
- (LowerList)checkList {LowerList temp;if (firstPicture) {temp = Left;firstPicture = NO;} else {leftHeight = leftView.frame.size.height;rightHeight = rightView.frame.size.height;if (leftHeight > rightHeight) {temp = Right;} else {temp = Left;}}return temp;
}
- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}

实现的效果是顺序把图片添加进界面

这篇关于使用串行线程实现图片瀑布流加载的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1040320

相关文章

C++变换迭代器使用方法小结

《C++变换迭代器使用方法小结》本文主要介绍了C++变换迭代器使用方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1、源码2、代码解析代码解析:transform_iterator1. transform_iterat

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进

C++中std::distance使用方法示例

《C++中std::distance使用方法示例》std::distance是C++标准库中的一个函数,用于计算两个迭代器之间的距离,本文主要介绍了C++中std::distance使用方法示例,具... 目录语法使用方式解释示例输出:其他说明:总结std::distance&n编程bsp;是 C++ 标准

vue使用docxtemplater导出word

《vue使用docxtemplater导出word》docxtemplater是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容,下面我们来看看如何使用docxtempl... 目录docxtemplatervue使用docxtemplater导出word安装常用语法 封装导出方

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

Linux换行符的使用方法详解

《Linux换行符的使用方法详解》本文介绍了Linux中常用的换行符LF及其在文件中的表示,展示了如何使用sed命令替换换行符,并列举了与换行符处理相关的Linux命令,通过代码讲解的非常详细,需要的... 目录简介检测文件中的换行符使用 cat -A 查看换行符使用 od -c 检查字符换行符格式转换将

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co