ios開發(fā)怎么獲取網(wǎng)頁上的全部圖片

這篇文章主要介紹“ios開發(fā)怎么獲取網(wǎng)頁上的全部圖片”,在日常操作中,相信很多人在ios開發(fā)怎么獲取網(wǎng)頁上的全部圖片問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”ios開發(fā)怎么獲取網(wǎng)頁上的全部圖片”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

創(chuàng)新互聯(lián)堅信:善待客戶,將會成為終身客戶。我們能堅持多年,是因為我們一直可值得信賴。我們從不忽悠初訪客戶,我們用心做好本職工作,不忘初心,方得始終。10余年網(wǎng)站建設經(jīng)驗創(chuàng)新互聯(lián)是成都老牌網(wǎng)站營銷服務商,為您提供網(wǎng)站設計、成都做網(wǎng)站、網(wǎng)站設計、HTML5建站、網(wǎng)站制作、成都品牌網(wǎng)站建設、小程序開發(fā)服務,給眾多知名企業(yè)提供過好品質的建站服務。

為了實現(xiàn) 使用WebView展示的網(wǎng)頁的時候  可以把網(wǎng)頁上的所有的圖片給提取出來  并且可以給WebView添加點擊時間 當點擊到圖片的時候 可以獲取出改圖片的Url,下面是方法的介紹

1、首先到如第三方庫 TFHpple

2、

//獲取網(wǎng)頁上全部的圖片

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

   //在頁面中查找img標簽

    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)頁里面的img標簽里面的關于圖片url那個標簽  可能是一般是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"];

       //判斷圖片的下載地址是相對路徑還是絕對路徑,如果是以http開頭,則是絕對地址,否則是相對地址

            if ([prefix isEqualToString:@"http"] == NO){

                url = [path stringByAppendingPathComponent:url];

            }

            if(url != nil){

                [_downloadImages addObject:url];

            }

    }

    }

   return_downloadImages;

}

//給webVeiw添加點擊時間 回去點擊位置的圖片的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;

}

//獲取點擊的圖片

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

        }

}

到此,關于“ios開發(fā)怎么獲取網(wǎng)頁上的全部圖片”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

當前標題:ios開發(fā)怎么獲取網(wǎng)頁上的全部圖片
文章轉載:http://muchs.cn/article32/pidesc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設、動態(tài)網(wǎng)站、ChatGPT、外貿(mào)網(wǎng)站建設、企業(yè)建站、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設公司