从Web上加载UIImage的最常见方法 [英] The most common way to load UIImage from the web
问题描述
亲爱的stackoverflowers
我走了。
在我的应用程式中, web,像这样:
- (void)loadImages
{
...
image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl1]];
image2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl2]];
}
为了不阻塞主线程,我使用 GCD :
dispatch_async(dispatch_get_global_queue(0,0),^ {
[self loadImages]; $ b在这之后,我使用这些图像在我的 UITableView
< : if(indexPath.row == 0)
{
cell.imageView.image = image1 ;
}
else
{
cell.imageView.image = image2;
}
然后我决定添加 UIActivityIndicator
,但是遇到一些问题,我理解我的代码不正确,使用 NSURLRequest
和 NSURLConnection
加载图像并添加 UIActivityIndicator
。
您能告诉我什么是最重要的加载图片的方式?我应该重写什么?
解决方案我建议你使用AsyncImageView一个漂亮的实现由Nicklockwood的iCarousel的父亲。
https://github.com/nicklockwood/AsyncImageView
它是非常简单。
#importAsyncImageView.h
$ [imageView setImage:p $ p>
@default.png];
[imageView setImageURL:@whateverurl / whateverimage.png];
在你的情况下,它将是:
[cell.imageView setImageURL:@yourURL ];
它的作用就像一个charm,我的代码正在生产中。但是如果你仍然希望你的代码工作,请尝试这样:
UIActivityIndicator * activity = [[UIActivityIndicator alloc] initWithStyle:UIActivityIndicatorWhite];
[activity setFrame:CGRectMake(100,100,30,30]];
[self.view addSubview:activity];
dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
dispatch_async(dispatchQueue,^(void)
{
[activity startAnimating];
[self loadImages];
dispatch_sync(dispatch_get_main_queue(),^ {
[yourTableView reloadData];
[activity stopAnimating];
[activty setHidden:YES];
});
});
Dear stackoverflowers,
I have gone astray.
In my app I load 2 images from the web, like this:
-(void)loadImages
{
...
image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl1]];
image2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl2]];
}
In order to not blocking the main thread, I use GCD:
dispatch_async( dispatch_get_global_queue(0,0), ^{
[self loadImages];
After that, I use these images in my UITableView
:
if (indexPath.row == 0)
{
cell.imageView.image = image1;
}
else
{
cell.imageView.image = image2;
}
Then I decided to add UIActivityIndicator
, but faced some problems. I understand that my code is not correct. I saw that people using NSURLRequest
and NSURLConnection
to load images and add UIActivityIndicator
.
Could you tell me please, what is the most standart way to load images like that ? What should I rewrite ?
解决方案 I would suggest you to use AsyncImageView a beautiful implementation by Nicklockwood -father of iCarousel.
https://github.com/nicklockwood/AsyncImageView
it is very simple to use.
#import "AsyncImageView.h"
and in all imageViews do this.
[imageView setImage:@"default.png"];
[imageView setImageURL:@"whateverurl/whateverimage.png"];
In your case it would be:
[cell.imageView setImageURL:@"yourURL"];
It works like a charm, and my code is in production. But if you still want your code to work try this:
UIActivityIndicator *activity =[[UIActivityIndicator alloc] initWithStyle:UIActivityIndicatorWhite];
[activity setFrame:CGRectMake(100,100,30,30)];
[self.view addSubview:activity];
dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(dispatchQueue, ^(void)
{
[activity startAnimating];
[self loadImages];
dispatch_sync(dispatch_get_main_queue(), ^{
[yourTableView reloadData];
[activity stopAnimating];
[activty setHidden:YES];
});
});
这篇关于从Web上加载UIImage的最常见方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!