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

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

相关文章

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

oracle DBMS_SQL.PARSE的使用方法和示例

《oracleDBMS_SQL.PARSE的使用方法和示例》DBMS_SQL是Oracle数据库中的一个强大包,用于动态构建和执行SQL语句,DBMS_SQL.PARSE过程解析SQL语句或PL/S... 目录语法示例注意事项DBMS_SQL 是 oracle 数据库中的一个强大包,它允许动态地构建和执行

前端原生js实现拖拽排课效果实例

《前端原生js实现拖拽排课效果实例》:本文主要介绍如何实现一个简单的课程表拖拽功能,通过HTML、CSS和JavaScript的配合,我们实现了课程项的拖拽、放置和显示功能,文中通过实例代码介绍的... 目录1. 效果展示2. 效果分析2.1 关键点2.2 实现方法3. 代码实现3.1 html部分3.2

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2