您的位置:首页 > 其它

Vickate_多线程的小方法以及如何使用多线程解决同步请求图片的卡顿现象

2015-09-22 20:29 567 查看
@interface RootViewController ()

@property (nonatomic, retain) UIImageView *imageV;
@property (nonatomic, retain) UIImageView *iamgeView;

@end

@implementation RootViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self addSubview];
[self addImageView];
}

// 点击按钮播放一组图片
- (void)addSubview
{
self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 175, 265)];
_imageV.backgroundColor = [UIColor orangeColor];
[self.view addSubview:_imageV];
[_imageV release];

UIButton *button = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];
button.frame = CGRectMake(100, 390, 50, 50);
button.backgroundColor = [UIColor orangeColor];
[button addTarget:self action:@selector(actionButton:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];

NSMutableArray *array = [NSMutableArray array];
for (int i = 110; i < 114; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", i]];
[array addObject:image];
}
self.imageV.animationDuration = 0.3;
self.imageV.animationImages = array;
self.imageV.animationRepeatCount = 0;

UIButton *button2 = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];
button2.frame = CGRectMake(225, 390, 50, 50);
button2.backgroundColor = [UIColor orangeColor];
[button2 addTarget:self action:@selector(actionButton2:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button2];

#warning NSThread ----
// 设置多线程解决  既可以执行代码,又可以播放动画
//    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(actionButton2:) object:nil];
//    [thread start];
#warning NSObject ---
// 基类解决卡顿现象
//    [self performSelectorInBackground:@selector(actionButton2:) withObject:nil];

// NSOperation也是一个抽象类  没有实现具体的功能
// NSBlockOperation 在block中操作 (相当于任务)
// NSInvocationOperation 调用操作 (相当于任务)
// NSOperationQueue 线程队列
#warning 不是主线程的方法
// 创建一个 NSInvocationOperation任务
NSInvocationOperation *invocationOP = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(actionInvocation) object:nil];

// 创建一个block任务
NSBlockOperation *blockOP = [NSBlockOperation blockOperationWithBlock:^{
[self block];
}];

// 创建一个队列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:invocationOP];
[queue addOperation:blockOP];

}

- (void)actionInvocation
{
NSLog(@"%@ 是不是主线程%d",[NSThread currentThread],[NSThread isMainThread]);
}
- (void)block
{
NSLog(@"%@ 是不是主线程%d", [NSThread currentThread],[NSThread isMainThread]);
}
- (void)actionButton:(UIButton *)button
{
if (self.imageV.isAnimating == NO) {
[self.imageV startAnimating];
} else {
[self.imageV stopAnimating];
}
}

- (void)actionButton2:(UIButton *)button
{
@autoreleasepool {
for (int i = 0; i < 10000000; i++) {
NSLog(@"%d", i);
}
}
}

#warning  需求  进行同步请求一张图片 不产生屏幕卡顿
//  思路: 开启一个子线程 进行同步请求图片 图片到手后 把图片返回主线程 显示到IMageView上

- (void)addImageView
{
// 创建一个iamgeView 用于显示图片
self.iamgeView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 500, 275, 100)];
_iamgeView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:_iamgeView];
[_iamgeView release];

#warning 网络请求
// 创建一个任务
NSBlockOperation *blockOPr = [NSBlockOperation blockOperationWithBlock:^{
@autoreleasepool {
NSURL *url = [NSURL URLWithString:@"http://img3.cache.netease.com/photo/0026/2013-03-19/8QATT85A4CJ80026.jpg"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
[self performSelectorInBackground:@selector(actionImage:) withObject:data];
}
}];
// 创建一个队列
NSOperationQueue *queue1 = [[NSOperationQueue alloc] init];
[queue1 addOperation:blockOPr];

}
- (void)actionImage:(NSData *)data
{
_iamgeView.image = [UIImage imageWithData:data];
}

欢迎光顾iOS_Bay的博客,谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: