---
title: "ios多线程"
author: "xiaotongeye"
date: "2014-11-27T14:35:39Z"
tags: []
categories: ["编程语言"]
slug: "ios"
draft: false
lastmod: "2014-11-27T14:35:39Z"
id: 2161269
url: "https://xiaotongeye.iteye.com/blog/2161269"
---

最近写了一个东西需要加载网络上的图片，运行的时候一直卡到一个界面特别慢，所以学习了一下多线程，我写的代码如下
``` 

-(void)imageView:(NSString *)imageHttp :(id)view
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        UIImage *image = [uiimage imagewithdata:[nsdata datawithcontentsofurl:[nsurl urlwithstring:imagehttp]]];
        dispatch_async(dispatch_get_main_queue(), ^{
            UIImageView *imageView = [self imagebook:image];
            [view addsubview:imageview];
        });
    });
}

```

其中imageHttp为网址，将uiimage的实例化放到了后台运行，而uiimageview得加载放到主线程，这样就不会因为图片加载的慢而卡到一个界面了。