Iphone 从web加载UIImage的最常见方法

Iphone 从web加载UIImage的最常见方法,iphone,ios,objective-c,xcode,cocoa,Iphone,Ios,Objective C,Xcode,Cocoa,亲爱的飞越者: 我误入歧途了 在我的应用程序中,我从web加载了2幅图像,如下所示: -(void)loadImages { ... image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl1]]; image2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl2]]; } 为了不阻塞主线程,我使用了GCD: dispatch_

亲爱的飞越者:

我误入歧途了

在我的应用程序中,我从web加载了2幅图像,如下所示:

-(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];
之后,我在我的
UITableView
中使用这些图像:

if (indexPath.row == 0)
    {
        cell.imageView.image = image1;
    }
    else
    {
        cell.imageView.image = image2;
    }
然后我决定添加
UIActivityIndicator
,但遇到了一些问题。我知道我的代码不正确。我看到人们使用
NSURLRequest
NSURLConnection
加载图像并添加
UIActivityIndicator


你能告诉我,什么是最标准的方式来加载这样的图像?我应该重写什么

如果我想使用自己的自定义代码,我可以告诉你我通常做什么

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    NSData * imageData = [NSData dataWithContentsOfURL:_pictureURL];
    dispatch_async(dispatch_get_main_queue(), ^{
        imageView.image = [UIImage imageWithData:imageData];
    });
});

否则可以考虑添加方法

的类别
setImageWithURLRequest:placeholderImage:success:failure:

要查看
UIImageView
,,您的代码没有问题(假设您已经确认它可以正常工作)。仅仅因为其他人使用NSURLConnection和related并不会让你错

要使用活动指示器,请执行以下操作:

-(void)loadImages
{
  UIActivityIndicator *indicator = ...

  ...
  [indicator startAnimating];
  image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl1]];
  image2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl2]];
  [indicator stopAnimating];
}
小nit-我个人会使用:

   cell.imageView.image = (0 == indexPath.row ? image1 : image2)

因为似乎没有人喜欢C“条件表达式”。结束小细节。

我建议您使用AsyncImageView,这是iCarousel之父Nicklockwood的一个漂亮实现

它使用起来非常简单

    #import "AsyncImageView.h"
在所有的ImageView中都可以这样做

    [imageView setImage:@"default.png"];
    [imageView setImageURL:@"whateverurl/whateverimage.png"];
在您的情况下,它将是: [cell.imageView setImageURL:@“yourURL”]

它就像一个符咒,我的代码正在生产中。但如果您仍希望代码正常工作,请尝试以下方法:

 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];
 });
 }); 

谢谢GoZoner!但我不知道在这种情况下如何使用UIActivityIndicator,因为我不知道何时加载图像,所以我可以停止指示器的动画。我看到,如果您使用NSURLConnection,您可以使用标准方法,例如
-(void)connectionIDFinishLoading:(NSURLConnection*)连接
,并了解图像何时加载。谢谢你的提醒,我没有想到。
dataWithContentsOfURL:
是一种同步方法。此代码将阻止UI。即使在分派\u async()中调用,在
分派\u async
no中,您是对的(我忽略了loadImage在其内部被调用的事实。这将按顺序检索图像,而为每个图像请求生成一个线程更有效。+Erway软件,它在主线程上的何处?您是否阅读了OP并查看了调用
loadImages
的位置?谢谢,这是一个非常好的解决方案。您是否尝试了我的实现如果你对我的ans很冷淡的话,投票或者接受。