Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS- Pull HTML id to display an image in an UIImageView
    text
    copied!<p>I need to be able to pull the line of HTML below from my website and display the image in the imageView I have in my Storyboard. I already Have some code on how to pull text from the same website, could I use something like the other piece of code? Of course, without the striped part, that is only to get plain text. I would just load the link to the image, but the image changes depending on the song playing. I want to try and avoid just displaying the image in a UIWebView, I would much rather prefer displaying it in a UIImageView</p> <p>HTML code:</p> <pre><code>&lt;div id="album_cover"&gt; &lt;img height="160px" width="160px" id="imgcover" src="Link to image changed based on the song playing" alt="Loading..."&gt;&lt;/div&gt; </code></pre> <p>Objective C code:</p> <pre><code>-(void)viewDidLoad { self.urlForLink = @"http://cloudrad.io/pointzeroradio/player"; NSURL *myURL = [NSURL URLWithString: [self.urlForLink stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; NSURLRequest *request = [NSURLRequest requestWithURL:myURL]; [webViewForRecents loadRequest:request]; timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(recentTracksText) userInfo:nil repeats:YES]; } -(void)recentTracksText { NSString *textForBlog = [webViewForRecents stringByEvaluatingJavaScriptFromString:@"document.getElementById('current_song').textContent;"]; // Add this step for stripping the HTML from the text you received self.strippedTextForBlog = [self stringByStrippingHTMLFromString:textForBlog]; continuousLabel.text = self.strippedTextForBlog; } </code></pre> <p>Based off of elio.d answer. This works great for me and loads anywhere from 30 seconds to 3 minutes. If anyone has a way that will load quickly.</p> <pre><code>-(void)getImageCover { NSString * js = @"document.getElementById('imgcover').src;"; NSString * imgLink = [webViewForRecents stringByEvaluatingJavaScriptFromString:js]; NSURL * imgURL = [NSURL URLWithString:imgLink]; dispatch_queue_t exampleQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); dispatch_async(exampleQueue, ^{ NSData * imgData = [NSData dataWithContentsOfURL:imgURL]; UIImage * image = [UIImage imageWithData:imgData]; dispatch_queue_t queue = dispatch_get_main_queue(); dispatch_async(queue, ^{ [imageView setImage:image]; }); }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload