groue / GRMustache

Flexible and production-ready Mustache templates for MacOS Cocoa and iOS
http://mustache.github.com/
MIT License
1.44k stars 190 forks source link

Problem with IMG #36

Closed amitvyawahare closed 11 years ago

amitvyawahare commented 11 years ago

I am trying to add Image using GRMustache but I am not able to make it work. Can you please help, because i don't know what i am doing wrong. I am using following code,

NSString *imgURl = @"~/Desktop/HTMLRendering/JetsFootball.jpg";

NSDictionary *object = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:headline,imgURl, nil]
                                                   forKeys:[NSArray arrayWithObjects:@"headline", @"imgURl", nil]];

NSString* HTML = [GRMustacheTemplate renderObject:object fromString:@"<body><h2>{{headline}}</h2><div><img border=\"0\" src=\"{{imgURl}}\" width=\"300\" height=\"200\"></img></div> error:nil];
groue commented 11 years ago

Hi again !

You're rendering some UIWebView or NSWebView, don't you? You have an issue with your baseURL, not with Mustache.

My advice: first focus on plain HTML, without any Mustache rendering. Craft some HTML and tweak your web view setup until your image renders. Once done, you'll know what kind of data you'll provide to your template.

Tell me if your issue remains, but I'm sure that after a good check of NS/UIWebView documentation, your problem will vanish.

amitvyawahare commented 11 years ago

Thank you for quick response. I am using UIWebView and still not able to get image loding on UIWebView. What is the best way to load an image from an URL using GRMustache on UIWebView.

amitvyawahare commented 11 years ago

This is the code I am using

UIWebView* webView = [[UIWebView alloc] initWithFrame:self.view.frame];

NSString *headline = @"Jets’ mistake wouldn’t happen to Patriots";
NSString *para1 = @"New York Jets coach Rex Ryan is separated from Patriots coach Bill Belichick by 164 career wins, three Super Bowl rings, a series of empty boasts, .";
NSString *para2 = @"Ryan proved that again Wednesday when he announced that (Off The) Mark Sanchez would be his starting quarterback Sunday against the Jacksonville Jaguars.";
NSString *para3 = @"Apparently, being quarterback of the Jets is the NFL equivalent of being a Supreme Court justice. Once you have the job,";
NSString *para4 = @"The Jets’ savior signal-caller last Sunday wasn’t Sanchez or pious passer Tim Tebow, but third-stringer Greg McElroy.";
NSString *imgURl = @"/Users/avyawahare/Documents/bostonglob/JetsFootball.png";

NSDictionary *object = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:headline, para1,para2,para3,para4,imgURl, nil]
                                                   forKeys:[NSArray arrayWithObjects:@"headline", @"para1", @"para2",@"para3",@"para4", @"imgURl", nil]];

NSString* HTML = [GRMustacheTemplate renderObject:object fromString:@"<body><h2>{{headline}}</h2><div><img border=\"0\" src=\"{{imgURl}}\" width=\"300\" height=\"200\"></img></div><div><p>{{para1}}</p><p>{{para2}}</p><p>{{para3}}</p><p>{{para4}}</p></div></body>" error:nil];  

[webView loadHTMLString:HTML baseURL:[NSURL URLWithString:@"http://"]];
[self.view addSubview:webView];
groue commented 11 years ago

It looks that you did not take my advice seriously.

Again: forget about Mustache, and learn how to render images in a web view.

http://whathaveyoutried.com