2009-10-16 10 views
2

Helllo, Je voudrais montrer le contenu dans le UIWebView, que j'ai ajouté au projet. par exemple une image. Je sais comment définir un texte dans le UIWebView, mais je voudrais ajouter des images ou une vidéo que j'ai ajouté au projetCharger le contenu dans le UIWebView sur l'Iphone

meilleures salutations,

+0

question similaires [de réponse ici] (http://stackoverflow.com/questions/1501629/uiwebview-display-locally-stored-website-html- images-javascript/1501908). – Ramin

Répondre

4

Ok, je suppose que vous utilisent loadHTMLString pour définir le code HTML sur votre UIWebView. Par conséquent, pour référencer les images stockées dans votre ensemble d'applications, il vous suffit d'obtenir le bon chemin pour insérer votre balise img. Donc obtenir la chaîne de chemin:

NSString * imgPath = [[NSBundle mainBundle] pathForResource:nameOfImage ofType:@"jpg"]; 

Format du HTML avec une balise d'image:

NSString * html = NSString [[[NSString alloc] initWithFormat:@"<img src=\"file://%@\"/>", imgPath] autorelease]; 

Définissez ensuite votre code HTML sur l'affichage Web:

[self.webView loadHTMLString:html baseURL:nil]; 

Vous avez juste besoin d'ajouter le images aux ressources de votre application (nameOfImage dans le code ci-dessus) et cela fonctionnera bien.

+0

Pour obtenir l'URL du fichier, utilisez NSURL: http://developer.apple.com/iphone/library/documentation/cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/occ/clm/ NSURL/fileURLWithPath: –

+0

Mots clés: "que j'ai ajouté au projet" – RedBlueThing

0

Chargez votre HTML dans votre UIWebView comme ceci:

// assumes you have a UIWebView called 'webView', and an HTML string called 'html' 
NSString* imagePath = [[NSBundle mainBundle] resourcePath]; 
imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"]; 
imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; 
[webView loadHTMLString:html baseURL:[NSURL URLWithString:[NSString stringWithFormat:@"file:/%@//", imagePath]]]; 

Ensuite, vous pouvez simplement consulter les noms de fichiers de vos images:

<img src="your_image.png" /> 

Vous n'avez pas besoin de spécifier un chemin par rapport à images que vous avez mis dans des dossiers. Lorsque l'application est compilée, ils vont tous à la racine.


Crédit: Je volé ce d'ici: http://dblog.com.au/iphone-development/loading-local-files-into-uiwebview/

Questions connexes