异步加载图像

时间:2011-04-30 07:56:11

标签: iphone

我已经加载了一些图片请告诉我如何使用异步加载图像

2 个答案:

答案 0 :(得分:2)

假设您想要在 UIImageView 中显示下载的图像,请使用EGOImageView,这非常简单(只需使用占位符图像初始化它并设置要下载的图像的URL)。

http://developers.enormego.com/view/what_if_images_on_the_iphone_were_as_easy_as_html

如果您不想显示图像,但只想进行异步下载,请使用EGOImageLoader。

只需确保您的班级符合 EGOImageLoaderObserver 协议并实施2个委托方法即可...

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
       imageLoader = [EGOImageLoader sharedImageLoader]; // ivar, so we can remove the observer in the dealloc ...
    }
    return self;
}

- (void)dealloc
{
   [imageLoader removeObserver:self];
   [super dealloc];
}

- (void)viewDidLoad
{
   [super viewDidLoad];

   NSString *urlString = @"http://www.wimwauters.be/wp-content/uploads/2011/01/steve-jobs.jpg";
   NSURL *url = [NSURL URLWithString:urlString];
   [imageLoader loadImageForURL:url observer:self];            
}

#pragma mark - EGOImageLoaderObserver

- (void)imageLoaderDidLoad:(NSNotification*)notification 
{
   NSDictionary *userInfo = [notification userInfo];
   UIImage *theImage = [userInfo objectForKey:@"image"];

   // do something with the image, e.g. display it in an ImageView ...
}

- (void)imageLoaderDidFailToLoad:(NSNotification*)notification 
{
   NSDictionary *userInfo = [notification userInfo];
   NSError *error = [userInfo objectForKey:@"error"];
   if (error) 
   {
    // handle the error
   }
}

答案 1 :(得分:1)

您是在谈论在后台下载图片吗?

Jeff LaMarche的这个教程是一个很好的解决方案 - http://iphonedevelopment.blogspot.com/2010/05/downloading-images-for-table-without.html

相关问题