這篇文章主要介紹“ios開發(fā)怎么獲取網(wǎng)頁(yè)上的全部圖片”,在日常操作中,相信很多人在ios開發(fā)怎么獲取網(wǎng)頁(yè)上的全部圖片問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”ios開發(fā)怎么獲取網(wǎng)頁(yè)上的全部圖片”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
創(chuàng)新互聯(lián)堅(jiān)信:善待客戶,將會(huì)成為終身客戶。我們能堅(jiān)持多年,是因?yàn)槲覀円恢笨芍档眯刨嚒N覀儚牟缓鲇瞥踉L客戶,我們用心做好本職工作,不忘初心,方得始終。10余年網(wǎng)站建設(shè)經(jīng)驗(yàn)創(chuàng)新互聯(lián)是成都老牌網(wǎng)站營(yíng)銷服務(wù)商,為您提供網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、HTML5建站、網(wǎng)站制作、成都品牌網(wǎng)站建設(shè)、小程序開發(fā)服務(wù),給眾多知名企業(yè)提供過好品質(zhì)的建站服務(wù)。
為了實(shí)現(xiàn) 使用WebView展示的網(wǎng)頁(yè)的時(shí)候 可以把網(wǎng)頁(yè)上的所有的圖片給提取出來 并且可以給WebView添加點(diǎn)擊時(shí)間 當(dāng)點(diǎn)擊到圖片的時(shí)候 可以獲取出改圖片的Url,下面是方法的介紹
1、首先到如第三方庫(kù) TFHpple
2、
//獲取網(wǎng)頁(yè)上全部的圖片
-(void)dowmLoadPhoto{
NSURLRequest *request = [NSURLRequestrequestWithURL:[NSURLURLWithString:path]];
NSData *response = [NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil];
if (response == nil){
UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"警告!"message:@"無法連接到該網(wǎng)站!"delegate:nilcancelButtonTitle:@"確定"otherButtonTitles:@"取消",nil];
[alertView show];
return;
}
NSArray *p_w_picpathsData = [self parseData:response];
NSMutableArray *p_w_picpaths = [self downLoadPicture:p_w_picpathsData];
}
//解析html數(shù)據(jù)
- (NSArray*)parseData:(NSData*) data
{
TFHpple *doc = [[TFHpplealloc]initWithHTMLData:data];
//在頁(yè)面中查找img標(biāo)簽
NSArray *p_w_picpaths = [doc searchWithXPathQuery:@"//img"];
return p_w_picpaths;
}
//下載圖片的方法
- (NSMutableArray*)downLoadPicture:(NSArray *)p_w_picpaths
{
//創(chuàng)建存放UIImage的數(shù)組
_downloadImages = [[NSMutableArrayalloc]init];
_p_w_picpathViewArr =[[NSMutableArrayalloc]init];
for (int i = 0; i < [p_w_picpaths count]; i++)
{
// 查看網(wǎng)頁(yè)里面的img標(biāo)簽里面的關(guān)于圖片url那個(gè)標(biāo)簽 可能是一般是src 所以下面就是[[[p_w_picpaths objectAtIndex:i] objectForKey:@"src"] 如果是其他的就替換下就可以了
NSString *prefix = [[[p_w_picpathsobjectAtIndex:i]objectForKey:@"src"]substringToIndex:4];
NSString *url = [[p_w_picpaths objectAtIndex:i] objectForKey:@"src"];
//判斷圖片的下載地址是相對(duì)路徑還是絕對(duì)路徑,如果是以http開頭,則是絕對(duì)地址,否則是相對(duì)地址
if ([prefix isEqualToString:@"http"] == NO){
url = [path stringByAppendingPathComponent:url];
}
if(url != nil){
[_downloadImages addObject:url];
}
}
}
return_downloadImages;
}
//給webVeiw添加點(diǎn)擊時(shí)間 回去點(diǎn)擊位置的圖片的Url
-(void)addTapOnWebView
{
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleSingleTap:)];
[_wvaddGestureRecognizer:singleTap];
singleTap.delegate = self;
singleTap.cancelsTouchesInView =NO;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
returnYES;
}
//獲取點(diǎn)擊的圖片
-(void)handleSingleTap:(UITapGestureRecognizer *)sender
{
CGPoint pt = [sender locationInView:_wv];
NSString *imgURL = [NSStringstringWithFormat:@"document.elementFromPoint(%f, %f).src", pt.x, pt.y];
NSString *urlToSave = [_wvstringByEvaluatingJavaScriptFromString:imgURL];
if([urlToSave hasPrefix:@"http"])
{
int index = [_downloadImages indexOfObject:urlToSave];
int count = _downloadImages.count;
// 1.封裝圖片數(shù)據(jù)
NSMutableArray *photos = [[NSMutableArrayalloc]init];
NSMutableArray *_viewArr =[[NSMutableArrayalloc]init];
for (int i = 0; i<count-1; i++) {
MJPhoto *photo = [[MJPhoto alloc] init];
photo.url = [NSURL URLWithString:[_downloadImages objectAtIndex:i]] ; //圖片路徑
[photos addObject:photo];
}
}
到此,關(guān)于“ios開發(fā)怎么獲取網(wǎng)頁(yè)上的全部圖片”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
當(dāng)前標(biāo)題:ios開發(fā)怎么獲取網(wǎng)頁(yè)上的全部圖片
文章轉(zhuǎn)載:http://www.chinadenli.net/article32/pidesc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站、ChatGPT、外貿(mào)網(wǎng)站建設(shè)、企業(yè)建站、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)