一个有趣的多线程应用实例
2016-03-02 19:30
239 查看
这个实例主要实现了隔相同的时间一张张加载图片在用for循环创建6个不同的图片视图上,且点击屏幕可使未完成的进程被取消,并退出即未加载的图片停止加载,有兴趣的朋友可以一试。(相应的注意点都已经在代码中注释)
#import "ViewController.h"
#define kUrl @"http://store.storeimages.cdn-apple.com/8748/as-images.apple.com/is/image/AppleInc/aos/published/images/s/38/s38ga/rdgd/s38ga-rdgd-sel-201601?wid=848&hei=848&fmt=jpeg&qlt=80&op_sharpen=0&resMode=bicub&op_usm=0.5,0.5,0,0&iccEmbed=0&layer=comp&.v=1454777389943"
@interface
ViewController (){
int tagValue;
UIImage *image;
NSMutableArray *threadArray;
}
@end
@implementation ViewController
-(void)viewDidLoad{
[super
viewDidLoad];
self.view.backgroundColor = [UIColor
whiteColor];
tagValue = 100;
threadArray = [NSMutableArray
array];
self.edgesForExtendedLayout =
0;
self.title =
@"多张图片";
[self.navigationController.navigationBar
setTitleTextAttributes:@{NSFontAttributeName:[UIFont
boldSystemFontOfSize:24],NSForegroundColorAttributeName:[UIColor
greenColor]}];
// 创建多个UIimageview
for (int row =
0; row<3; row++) {
for (int list =
0; list<2; list++) {
UIImageView *imageView = [[UIImageView
alloc]initWithFrame:CGRectMake(10+list*200,
10+row*200,
200, 200)];
imageView.tag =
tagValue++;
imageView.backgroundColor = [UIColor
redColor];
imageView.userInteractionEnabled =
YES;
[self.view
addSubview:imageView];
}
}
// 创建多个子线程
for (int index =
0; index<6; index++) {
[NSThread
detachNewThreadSelector:@selector(thread:)
toTarget:self
withObject:@(index)];
}
}
// 通过url加载网络图片
- (void)thread:(NSNumber *)index{
[threadArray
addObject:[NSThread
currentThread]];
NSThread *thread = [NSThread
currentThread];
[NSThread
sleepForTimeInterval:[index intValue]];//通过线程休眠实现顺序加载
if (thread.isCancelled ==
YES) {
[NSThread
exit];
}
NSData *data = [NSData
dataWithContentsOfURL:[NSURL
URLWithString:kUrl]];
image = [UIImage
imageWithData:data];
// ❤️❤️回到主线程
[self
performSelectorOnMainThread:@selector(updateUI:)
withObject:index waitUntilDone:YES];
}
// 更新UI
- (void)updateUI:(NSNumber *)index{
UIImageView *imageV = [self.view
viewWithTag:[index
integerValue]+100];
imageV.image =
image;
}
//点击屏幕取消未完成的线程
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"%@",threadArray);
for (int index =
0; index<6; index++) {
NSThread *thread =
threadArray[index];
if (thread.isFinished ==
NO) {
[thread cancel];//取消不是退出
}
}
}
@end
#import "ViewController.h"
#define kUrl @"http://store.storeimages.cdn-apple.com/8748/as-images.apple.com/is/image/AppleInc/aos/published/images/s/38/s38ga/rdgd/s38ga-rdgd-sel-201601?wid=848&hei=848&fmt=jpeg&qlt=80&op_sharpen=0&resMode=bicub&op_usm=0.5,0.5,0,0&iccEmbed=0&layer=comp&.v=1454777389943"
@interface
ViewController (){
int tagValue;
UIImage *image;
NSMutableArray *threadArray;
}
@end
@implementation ViewController
-(void)viewDidLoad{
[super
viewDidLoad];
self.view.backgroundColor = [UIColor
whiteColor];
tagValue = 100;
threadArray = [NSMutableArray
array];
self.edgesForExtendedLayout =
0;
self.title =
@"多张图片";
[self.navigationController.navigationBar
setTitleTextAttributes:@{NSFontAttributeName:[UIFont
boldSystemFontOfSize:24],NSForegroundColorAttributeName:[UIColor
greenColor]}];
// 创建多个UIimageview
for (int row =
0; row<3; row++) {
for (int list =
0; list<2; list++) {
UIImageView *imageView = [[UIImageView
alloc]initWithFrame:CGRectMake(10+list*200,
10+row*200,
200, 200)];
imageView.tag =
tagValue++;
imageView.backgroundColor = [UIColor
redColor];
imageView.userInteractionEnabled =
YES;
[self.view
addSubview:imageView];
}
}
// 创建多个子线程
for (int index =
0; index<6; index++) {
[NSThread
detachNewThreadSelector:@selector(thread:)
toTarget:self
withObject:@(index)];
}
}
// 通过url加载网络图片
- (void)thread:(NSNumber *)index{
[threadArray
addObject:[NSThread
currentThread]];
NSThread *thread = [NSThread
currentThread];
[NSThread
sleepForTimeInterval:[index intValue]];//通过线程休眠实现顺序加载
if (thread.isCancelled ==
YES) {
[NSThread
exit];
}
NSData *data = [NSData
dataWithContentsOfURL:[NSURL
URLWithString:kUrl]];
image = [UIImage
imageWithData:data];
// ❤️❤️回到主线程
[self
performSelectorOnMainThread:@selector(updateUI:)
withObject:index waitUntilDone:YES];
}
// 更新UI
- (void)updateUI:(NSNumber *)index{
UIImageView *imageV = [self.view
viewWithTag:[index
integerValue]+100];
imageV.image =
image;
}
//点击屏幕取消未完成的线程
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"%@",threadArray);
for (int index =
0; index<6; index++) {
NSThread *thread =
threadArray[index];
if (thread.isFinished ==
NO) {
[thread cancel];//取消不是退出
}
}
}
@end
相关文章推荐
- Objective-C 和 Core Foundation 对象相互转换的内存管理总结
- maven 工程启动找不到 Spring ContextLoaderListener 的解决办法
- 建播时间 2/3/2016
- hadoop面试题20道
- hibernate笔记--组件映射方法
- 数据库安装检查
- 求绝对众数
- 丢点小东西
- FZU - 1707 (卡logN的题)
- Web Deploy发布网站一条龙解决方案
- Oracle 10g 11g 单机环境的安装
- python的命令模式和编辑模式
- ClockRepairs
- workman windows架构图
- 关于环境变量失效的问题
- 软件测试(一)之 error
- NYOJ-128-前缀式计算
- iOS(学习) UITabBarController
- RHEL 5 ORACLE Linux 5.9 配置YUM源 5配置yum
- Android 横竖屏切换