Afficher une image à  partir d'un URL dans une application iPhone

cybergodcybergod Membre
I have an iPhone application that reads an XML file from a server this file contains titles of articles, a description for every article and an image. I do parse a XML very well and all the fields are reachable (title, description...) even the image field. When I put the URL of the image from my XML in a label in order to verify if the URL is parsed I get the URL without a problem. The problem is that when the imageView takes the URL as a source of the image I don't get the image but when I write the same URL as a string I do have image in my View.

Here is my Code that I'm using.


//  FirstViewController.m


// Returns cell to render for each row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @MyIdentifier;
MyIdentifier = @tblCellView;
TableCellView *cell = (TableCellView *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil) {
  [[NSBundle mainBundle] loadNibNamed:@TableCellView owner:self options:nil];
  cell = tblCell;
}

//Here the XMLparser
item *aBook = [appDelegate.books objectAtIndex:indexPath.row]; 
//Creation of the image that will take the URL (aBook.image) as a source
UIImage *image = UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:aBook.image];
[cell setDescpText2:aBook.title];
[cell setLabelText:aBook.date];
[cell setDescpText:aBook.description];
[cell setProductImage:image];
//All the fields do appear in my application properly only the image doesn't. When I write the content of the aBook.image in a Label I do see the URL of my image.
return cell;
}
//Even when I show the aBook.image in the NsLog the URL is the right one but only when I call it into the image that I don't get anything

Thank you for any help that could solve this problem.

Réponses

  • AliGatorAliGator Membre, Modérateur
    08:10 modifié #2
    Hi and welcome,

    Did you check the different values returned by the steps you are using to generate the image ?
    - does [NSURL URLWithString:aBook.image] return an correct NSURL object or does it return nil (meaning the string representing the URL in aBook.image is not a valid URL complient with the standard for URLs for example)
    - if the NSURL objet is not nil, does then the [NSData dataWithContentsOfURL:...) return valid data or does it return nil ? If it returns nil, it could mean multiple things, for example the URL may lead to a 404 error (image not found) or sthg similar
    - if the NSData is not nil, is the UIImage nil or valid too ?


    This will help you see where the error is located (when creating the NSURL object, when retrieving the NSData, or when building the UIImage from the NSData ?)

    Moreover, it is a bad idea to perform a synchronous request (using dataWithContentsOfURL) as such requests block the iPhone until the data has been downloaded. During the download, the user cannot do anything as the iPhone is blocked by the download task, and this is espacially bad when (1) the network connection of the user is down or very slow (2) you have multiple images to download and thus chain the synchronous requests.

    You should use asynchronous requests for your case. You also should take a look at the LazyTableView sample code provided by Apple on their developer's site, which explore exactly the same king of project your are doing, loading images lazying and asynchronously to display them in a tableview without blocking the runloop, thus the user, user interaction, and especially tableview scrolling by downloading images in a blocking way.


    Finally, this forum is a french Cocoa forum, and all our members speak french here.
    I have been kind enough to answer your question in english because it is your first post in this forum, but please use french next time ; if you don't speak french, you may consider asking your question in another forum : not that you're not welcome here, we are welcome to help you... but the problem is that most of our users won't be able to answer your questions as they do not speak english so fluently, and english cocoa forums may suit your needs better.

    Sincerly
  • cybergodcybergod Membre
    08:10 modifié #3
    Merci bien pour ton aide.
    Voici une version française de mon problème.

    J'ai une application iPhone qui lit un fichier XML à  partir d'un serveur, ce fichier contient les titres des articles, une description de chaque article et une image. La lecture du fichier XML se fait comme il faut et tous les champs sont accessibles (titre, description ...) même le champ de l'image. Quand je mets l'URL de l'image de mon XML dans une étiquette afin de vérifier si l'URL est analysée j'obtient l'URL sans problème. Le problème est que lorsque le imageView prend l'URL comme une source de l'image je ne reçois pas l'image, mais quand j'écris la même URL en tant que chaà®ne J'ai l'image. Voilà  mon code :

    <br />//&nbsp; FirstViewController.m<br /><br /><br />// Returns cell to render for each row<br />- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { <br /> static NSString *MyIdentifier = @&quot;MyIdentifier&quot;;<br /> MyIdentifier = @&quot;tblCellView&quot;; <br /> TableCellView *cell = (TableCellView *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];<br /> if(cell == nil) {<br />&nbsp; [[NSBundle mainBundle] loadNibNamed:@&quot;TableCellView&quot; owner:self options:nil];<br />&nbsp; cell = tblCell;<br /> }<br /> <br />//Voici le Parser du XML<br /> item *aBook = [appDelegate.books objectAtIndex:indexPath.row];&nbsp; <br />//Creation of d l&#39;image qui va prendre l&#39;URL (aBook.image) comme source<br />UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:aBook.image]]];<br />[cell setDescpText2:aBook.title];<br /> [cell setLabelText:aBook.date];<br /> [cell setDescpText:aBook.description];<br /> [cell setProductImage:image]; <br />//Quand j'ecris le contenu du aBook.image dans une Label, l'URl s'affiche bien mais l'image n'apparit jamais.<br /> return cell;<br />}<br />
    


    Je vais essayer d'analyser mon code avec la façon que tu m'as indiqué et je vais aussi essayer de voir le lazyTableView de Apple.
    Merci.
Connectez-vous ou Inscrivez-vous pour répondre.