gpt4 book ai didi

html - iOS- 拉取 HTML id 以在 UIImageView 中显示图像

转载 作者:行者123 更新时间:2023-11-29 03:28:18 24 4
gpt4 key购买 nike

我需要能够从我的网站中提取下面的 HTML 行,并在 Storyboard 中的 imageView 中显示图像。我已经有一些关于如何从同一网站提取文本的代码,我可以使用类似另一段代码的代码吗?当然,如果没有条纹部分,那只是得到纯文本。我只会加载图像的链接,但图像会根据播放的歌曲而变化。我想尝试避免只在 UIWebView 中显示图像,我更喜欢在 UIImageView 中显示它

HTML 代码:

<div id="album_cover"> 
<img height="160px" width="160px" id="imgcover" src="Link to image changed based on the song playing" alt="Loading..."></div>

objective-c 代码:

-(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;


}

基于 Elio.d 的回答。这对我来说非常有用,加载时间从 30 秒到 3 分钟不等。如果有人有一种可以快速加载的方法。

-(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];

});
});
}

最佳答案

我想你的 div 只包含一个图像

如果是这样,这就是您可以实现的方式:

-(void)getImageCover {
NSString * js = @"document.getElementById("imgcover").src;"
NSString * imgLink = [webViewForRecents stringByEvaluatingString:js];
NSURL * imgURL = [NSURL urlWithString:imgLink];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSData * imgData = [NSData dataWithContentsOfURL:imgURL];
UIImage * image = [UIImage imageWithData:imageData];
dispatch_async(dispatch_get_main_queue(),{
[imageView setImage:image];
});
});
}

关于html - iOS- 拉取 HTML id 以在 UIImageView 中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20165442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com