- 浏览: 513814 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
tangyunliang:
大哥你太历害了谢谢
Android基于XMPP Smack Openfire开发IM【四】初步实现两个客户端通信 -
u013015029:
LZ,请问下,在// 添加消息到聊天窗口 , 这里获取Ed ...
Android基于XMPP Smack Openfire开发IM【四】初步实现两个客户端通信 -
endual:
怎么保持会话,我搞不懂啊
Android基于XMPP Smack Openfire开发IM【一】登录openfire服务器 -
donala_zq:
显示:[2013-11-30 11:50:36 - Andro ...
android-----------新浪微博 -
donala_zq:
哥,运行不了啊
android-----------新浪微博
实现的功能:1)演示多线程NSThread开发;2)子线程中执行下载图片工作,图片下载完成前显示等待框;
关键词:多线程 NSThread 等待框
运行效果图:
[img]
[/img]
[img]
[/img]
1、新建视图控制器ViewController(不带xib),作为根视图控制器,通过ViewController的-(void)loadView方法构建UI,ViewController.h如下:
ViewController.m如下:
关键词:多线程 NSThread 等待框
运行效果图:
[img]
[/img]
[img]
[/img]
1、新建视图控制器ViewController(不带xib),作为根视图控制器,通过ViewController的-(void)loadView方法构建UI,ViewController.h如下:
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<NSURLConnectionDelegate> @property(strong,nonatomic)UIImageView *imageView; @property(strong,nonatomic)NSMutableData *buffer; @property Boolean done; @end
ViewController.m如下:
#import "ViewController.h" @implementation ViewController @synthesize imageView; @synthesize buffer; @synthesize done; -(void)loadView{ //初始化视图 [self initViews]; NSString *url = @"http://avatar.csdn.net/4/D/5/1_m_changgong.jpg"; //在子线程执行图片下载操作 [NSThread detachNewThreadSelector:@selector(downloadAppImage:) toTarget:self withObject:url]; } //初始化视图组件 -(void)initViews{ CGRect frame = [UIScreen mainScreen].applicationFrame; imageView = [[UIImageView alloc]initWithFrame:frame]; self.view = imageView; [self.view setBackgroundColor:[UIColor whiteColor]]; } //下载图片 -(void)downloadAppImage:(NSString *) url{ //展示等待框 [self showWaiting]; done = NO; //初始化缓冲区 buffer = [NSMutableData data]; [[NSURLCache sharedURLCache] removeAllCachedResponses]; //设置请求及连接 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]]; NSURLConnection *connnection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; if(connnection!=nil){ while(!done){ //NSLog(@"doing.."); [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; } } //生成UIImage实例 UIImage *img = [UIImage imageWithData:buffer]; //通知主线程 [self performSelectorOnMainThread:@selector(showImage:) withObject:img waitUntilDone:NO]; request = nil; connnection = nil; } //填充图片 -(void)showImage:(UIImage *)img{ [self.imageView setImage:img]; //关闭等待框 [self hiddenWaiting]; } -(void)httpConnectEnd{ NSLog(@"httpConnectEnd"); } -(void)httpConnectEndWithError{ [self hiddenWaiting]; NSLog(@"httpConnectEndWithError"); } //展示等待框 -(void)showWaiting{ int width = 32; int height = 32; CGRect frame = CGRectMake(0, -20, 320, 480); int x = frame.size.width; int y = frame.size.height; NSLog(@"x=%d,y=%d",x,y); frame = CGRectMake((x-width)/2, (y-height)/2, width, height); UIActivityIndicatorView *progressInd = [[UIActivityIndicatorView alloc]initWithFrame:frame]; [progressInd startAnimating]; progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge; frame = CGRectMake((x-70)/2, (y-height)/2+height, 80, 20); UILabel *waitinglabel = [[UILabel alloc]initWithFrame:frame]; waitinglabel.text = @"正在下载应用程序图片..."; waitinglabel.textColor = [UIColor redColor]; waitinglabel.font = [UIFont systemFontOfSize:15]; waitinglabel.backgroundColor = [UIColor clearColor]; frame = CGRectMake(0, -20, 320, 480); UIView *waitingView = [[UIView alloc]initWithFrame:frame]; waitingView.backgroundColor = [UIColor blackColor]; waitingView.alpha = 0.7; [waitingView addSubview:progressInd]; [waitingView addSubview:waitinglabel]; [waitingView setTag:110];//设置tag [self.view addSubview:waitingView]; } //隐藏等待框 -(void)hiddenWaiting{ //根据tag找到waitingView [[self.view viewWithTag:110]removeFromSuperview]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } #pragma mark NSURLConnection Delegate methods //不执行缓存 -(NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse{ return nil; } //连接发生错误 -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ [self performSelectorOnMainThread:@selector(httpConnectEndWithError) withObject:self waitUntilDone:NO]; [buffer setLength:0]; } //接收数据 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [buffer appendData:data]; } //下载完毕 -(void)connectionDidFinishLoading:(NSURLConnection *)connection{ //NSLog(@"connectionDidFinishLoading"); [self performSelectorOnMainThread:@selector(httpConnectEnd) withObject:nil waitUntilDone:NO]; //self.done = YES; } @end
发表评论
-
新风作浪博客学习(十九)在iOS虚拟键盘上添加动态隐藏按钮
2013-06-08 09:19 808为了给用户比较良好的交付,想在键盘上添加一个按钮,实时根据键盘 ... -
新风作浪博客学习(十八)openURL的使用(iOS调用系统电话、浏览器、地图、邮件等) .
2013-06-08 09:19 922今天遇见一行代码实现打开一个网页,比起印象里的UIWebVie ... -
新风作浪博客学习(十七)UIImageView响应点击事件 .
2013-06-08 09:19 661有时候会遇到点击一张图片,然后让这张图片触发一个事件,或者是跳 ... -
新风作浪博客学习(十六)Navigation + Tab Bar 常用组合框架 .
2013-06-07 08:50 1188看到很多项目中都采用的是Navigation加Tab Bar组 ... -
新风作浪博客学习(十五)google地图定位小Demo .
2013-06-07 08:50 1050[img][/img]今天写的是一个简单功能的google地图 ... -
新风作浪博客学习(十四)怎样向iPhone模拟器中添加图片 .
2013-06-07 08:50 738在我们做项目中可能需要使用图库,模拟器是有图库的,但是如何像其 ... -
新风作浪博客学习(十三)表视图的分组分区和索引分区 .
2013-06-07 08:50 736本次实现的是表视图的分区和索引,代码和前面都差不多,主要还是代 ... -
新风作浪博客学习(十二)代码实现UITableViewCell表视图单元定制 .
2013-06-07 08:49 925通常情况下我们会希望单元格UITableViewCell显示自 ... -
新风作浪博客学习(十一)UITableViewCell的标记、移动、删除、插入 .
2013-06-06 09:15 1050这篇文章是建立在 代码实现 UITableView与UITa ... -
新风作浪博客学习(十)代码实现 UITableView与UITableViewCell .
2013-06-06 09:14 1106我们常用的表格类视图就是用 UITableView与UITab ... -
新风作浪博客学习(九)两个UIPickerView控件间的数据依赖 .
2013-06-06 09:14 1034本篇实现功能是两个选取器的关联操作,滚动第一个滚轮第二个滚 ... -
新风作浪博客学习(八)代码实现UIPickerView .
2013-06-06 09:14 1230先说一下当个组件选取器,我们创建一个数组NSAray来保存选取 ... -
新风作浪博客学习(七)代码 实现UIDatePicker控件 和 Tab Bar 视图切换 .
2013-06-06 09:15 1059感觉代码写控件都一个理,先在ViewDidLoad中创建控件对 ... -
新风作浪博客学习(六)ios 视图切换翻页效果 .
2013-06-05 11:18 1016本文写的是视图切换,涉及到的内容有 1.实现代码添加Navi ... -
新风作浪博客学习(五)代码实现UISlider 和 UISwitch .
2013-02-18 09:15 1067本次实现的UISlider和UISwi ... -
新风作浪博客学习(四)把plist里数据显示在textField上 .
2013-02-18 09:15 874在代码实现Lable 、textFie ... -
新风作浪博客学习(三)NSBundle读取图片 plist文件和txt文件
2013-02-18 09:15 1611本文想简单介绍一下NSBundle读取图片到视图上,读取pli ... -
新风作浪博客学习(二)代码实现Lable 、textField创建界面以及键盘的处理
2013-02-18 09:15 1132今天写的是用代码实现一个简单界面,代码重复率比较高,可读性不是 ... -
新风作浪博客学习(一)plist文件读写操作
2013-02-18 09:14 1288文件plist 全名Property List,属性列表文件, ... -
GCDiscreetNotificationView提示视图
2013-06-05 11:17 519先看一下效果图: [img] ...
相关推荐
IOS应用源码——ui多线程的简单例子 NSThread.zip
iOS多线程开发NSThread.pdf
IOS应用源码——NSThread.zip
iOS多线程编程技术之NSThread、Cocoa NSOperation、GCD
iOS多线程开发一 使用NSThread NSThread的基本使用 // demo说明 NSThreadDemoOne: 简单使用多线程,区分有多线程和没有多线的区别 NSThreadDemoTwo: 因为NSThread只能传一个一个参数,如果咬传递多个参数,使用封装...
今天写这个demo就是为了理解多线程的概念并记录下一些NSThread的基本操作。总共开启了3个子线程,其中两个线程陷入死循环模拟耗时操作,并由bool变量run控制同步更新UI操作。另外一个线程则实现了从网上下载图片并将...
iOS多线程,四种实现方式中最简单的两种pthread,NSThread的用法介绍。
在iOS开发中,多线程的实现方式主要有三种,NSThread、NSOperation和GCD,我前面博客中对NSOperation和GCD有了较为详细的实现,为了学习的完整性,今天我们主要从代码层面来实现NSThread的使用。案例代码上传至 ...
iphone开发之多线程NSThread和NSInvocationOperation 的 使用
主要介绍了详解iOS多线程之2.NSThread的加锁@synchronized,有需要的小伙伴可以参考下。
多线程加载图片
2.2.1 使用 NSThread.............................................................................................. 14 2.2.2 使用 POSIX 的多线程 ...........................................................
该demo详细解释了gcd NSThread NSOperationQueue的用法.详细解释了子线程中如何刷新UI
ios多线程编程指南,包含NSThread,CGD等,深入简出讲解ios多线程原理和应用
ios多线程开发的常用四种方式和基本使用,pthread、NSThread、NSOperation\NSOperationQueue、GCD
在实际应用中,多线程技术也有着非常多的使用场景,比如异步的网络请求、异步的图片加载、后台执行复杂任务等等。 在iOS相关技术岗位的面试中,多线程技术更是必考项。作为开发者,我们除了能够熟练使用常用的多线程...
iOS多线程编程之NSThread的使用案例源码,1、简介:1.1 iOS有三种多线程编程的技术,分别是:1.、NSThread 。2、Cocoa NSOperation (iOS多线程编程之NSOperation和NSOperationQueue的使用)3、GCD 全称:Grand ...
NSThread下载图片