2010-10-01 14 views
2

J'ai le code suivant qui charge sur un UIImageView l'image que je veux de l'internet:chaîne lecture à partir d'une URL et le chargement alors il est l'image dans un UIImageView

NSString* mapURL = @"http://mydomain.com/image-320x480.png"; 
NSData* imageData = [[NSData alloc]initWithContentsOfURL:url]; 

UIImage* image = [[UIImage alloc] initWithData:imageData]; 
[marcaBackground setImage:image]; 
[imageData release]; 
[image release]; 

Je voudrais plutôt coder en dur l'URL de l'image, charger une chaîne d'une autre URL, puis charger cette image sur UIImageView.

Mon URL renvoie juste une autre URL au format texte.

Par exemple: http://mydomain.com/url retours http://mydomain.com/image-320x480.png

Comment pourrais-je accomplir cette tâche?

+0

Cela semble assez salissant. Pourquoi ne pas rendre 'http: // mydomain.com/url' renvoyer une redirection HTTP correcte plutôt que votre propre schéma personnalisé? –

Répondre

3

Utilisation:

NSData *imageData = 
    [[NSData alloc] initWithContentsOfUrl: 
     [NSString 
      stringWithContentsOfUrl: 
      [NSURL URLWithString:@"http://mydomain.com"] 
      encoding: NSUTF8StringEncoding 
      error: nil 
     ] 
    ]; 

Procédez ensuite comme vous étiez déjà en train de faire.

UIImage* image = [[UIImage alloc] initWithData:imageData]; 
[marcaBackground setImage:image]; 
[imageData release]; 
[image release]; 
+0

Excellente réponse. Juste besoin de correction de typo à initWithContentsOfUrl pour initWithContentsOfURL. Merci! –