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

本文介绍了一个iOS应用中实现图片动画与多线程处理的方法。通过使用UIImageView展示动画,并结合NSOperationQueue来确保动画流畅运行的同时进行大量计算任务,避免了UI卡顿。此外,还介绍了如何通过子线程同步请求图片并将其安全地显示在主线程。
@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的博客,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值