IOS多线程下载图片

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{
    UIImageView *imageView;
}


@property(nonatomic, retain) IBOutlet UIImageView *imageView;

@end

上面定义一个imageView

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize imageView;
- (void)viewDidLoad {
    [super viewDidLoad];
     NSString *url  = @"http://d.hiphotos.baidu.com/image/w%3D1366%3Bcrop%3D0%2C0%2C1366%2C768/sign=6cbcab9dabec8a13141a53e3c135aaec/aa64034f78f0f7369453c3730855b319ebc41316.jpg" ;

    // Do any additional setup after loading the view, typically from a nib.
    UIButton *myDemoButton = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, 200, 50)];
    [myDemoButton setBackgroundColor:[UIColor purpleColor]];
    [myDemoButton addTarget:self action:@selector(myButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:myDemoButton];
//    [self downLoadImage:[NSString stringWithFormat:@"%s", IMAGE_VIEW]];

    NSThread *myThread = [[NSThread alloc] initWithTarget:self selector:@selector(downLoadImage:) object:url];
    [myThread start];
}
- (void)myButtonPressed:(id)sender{
    NSLog(@"myDemoButton pressed");


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)downLoadImage:(NSString *)url{
    NSData *imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:url]];
    sleep(5);
    UIImage *imageImage = [[UIImage alloc] initWithData:imageData];
    if(imageImage){
        [self performSelectorOnMainThread:@selector(updateUI:) withObject:imageImage waitUntilDone:YES];
    }
}

- (void)updateUI:(UIImage *)image{
    self.imageView.image = image;

}
@end

点击骚紫色button显示图片
mac电脑不太会操作就不截图了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我扶奶奶过哈登

您的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值