更快地从URL加载图像并将其显示在iPhone应用程序中

我正在使用以下代码在我的ImageView中显示图像:

imgbackBG.image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", [test.arrImagessplash objectAtIndex:[test.arrImages count]-4]]]]]; 4cing.com/mobile_app/uploads/pageicon/splash.png 

问题是代码执行非常缓慢。 有没有办法加载图像,并更快地显示在ImageView? 如果是这样,我该怎么做?

从这里下载文件…..

https://github.com/nicklockwood/AsyncImageView

并按以下方式使用:

 AsyncImageView *asyncImageView = [[AsyncImageView alloc]initWithFrame:CGRectMake(30,32,100, 100)]; [asyncImageView loadImageFromURL:[NSURL URLWithString:your url]]; [YourImageView addSubview:asyncImageView]; [asyncImageView release]; 

你正在使用的代码在主线程中加载图像。 这将阻止用户界面。 使用GCDasynchronous加载图像。

这里是示例代码:

 dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); dispatch_async(q, ^{ /* Fetch the image from the server... */ NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *img = [[UIImage alloc] initWithData:data]; dispatch_async(dispatch_get_main_queue(), ^{ /* This is the main thread again, where we set the tableView's image to be what we just fetched. */ cell.imgview.image = img; }); }); 
 Best way to used `NSOperationQueue` to load Image in background. Here is the sample code: NSOperationQueue *myQueue = [[NSOperationQueue alloc] init]; [myQueue addOperationWithBlock:^{ UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@""]]]?:[UIImage imageNamed:@"defaultImage.png"]; [[NSOperationQueue mainQueue] addOperationWithBlock:^{ YourImageView.image = img; }]; }]; 

为图像caching使用SdwebImage框架

https://github.com/rs/SDWebImage