2011-04-30 4 views
2

J'ai chargé quelques images s'il vous plaît dites-moi comment utiliser le chargement asynchrone des imagesChargement asynchrone des images

+2

Voulez-vous expliquer passer quelques plus de mots? – gd1

+0

je reçois des images json je veux charger les images mais très le temps consomme donc je ne sais pas comment utiliser les méthodes asynchrones pour charger les images – user732177

Répondre

2

Faire usage de EGOImageView en supposant que vous souhaitez afficher l'image téléchargée dans un UIImageView, il est vraiment facile (initialize juste avec une image d'espace réservé et réglé l'URL de l'image à télécharger).

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

Si vous ne souhaitez pas afficher l'image, mais juste voulez télécharger asynchrone, utiliser EGOImageLoader.

Assurez-vous que votre classe est conforme au protocole EGOImageLoaderObserver et mettre en œuvre des méthodes 2 délégués et vous avez terminé ...

- (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 
    } 
}