资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

ios开发怎么获取网页上的全部图片

这篇文章主要介绍“ios开发怎么获取网页上的全部图片”,在日常操作中,相信很多人在ios开发怎么获取网页上的全部图片问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”ios开发怎么获取网页上的全部图片”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

创新互联坚信:善待客户,将会成为终身客户。我们能坚持多年,是因为我们一直可值得信赖。我们从不忽悠初访客户,我们用心做好本职工作,不忘初心,方得始终。10余年网站建设经验创新互联是成都老牌网站营销服务商,为您提供网站设计、成都做网站、网站设计、HTML5建站、网站制作、成都品牌网站建设小程序开发服务,给众多知名企业提供过好品质的建站服务。

为了实现 使用WebView展示的网页的时候  可以把网页上的所有的图片给提取出来  并且可以给WebView添加点击时间 当点击到图片的时候 可以获取出改图片的Url,下面是方法的介绍

1、首先到如第三方库 TFHpple

2、

//获取网页上全部的图片

-(void)dowmLoadPhoto{

   NSURLRequest *request = [NSURLRequestrequestWithURL:[NSURLURLWithString:path]];

   NSData *response = [NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil];

    if (response == nil){

       UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"警告!"message:@"无法连接到该网站!"delegate:nilcancelButtonTitle:@"确定"otherButtonTitles:@"取消",nil];

        [alertView show];

        return;

    }

    NSArray *p_w_picpathsData =  [self parseData:response];

    NSMutableArray *p_w_picpaths = [self downLoadPicture:p_w_picpathsData];

}

//解析html数据

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

{

   //创建存放UIImage的数组

   _downloadImages = [[NSMutableArrayalloc]init];

   _p_w_picpathViewArr =[[NSMutableArrayalloc]init];

        for (int i = 0; i < [p_w_picpaths count]; i++)

        {

// 查看网页里面的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.封装图片数据

   NSMutableArray *photos = [[NSMutableArrayalloc]init];

   NSMutableArray *_viewArr =[[NSMutableArrayalloc]init];

        for (int i = 0; i

                MJPhoto *photo = [[MJPhoto alloc] init];

                photo.url = [NSURL URLWithString:[_downloadImages objectAtIndex:i]] ; //图片路径

                [photos addObject:photo];

        }

}

到此,关于“ios开发怎么获取网页上的全部图片”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


本文题目:ios开发怎么获取网页上的全部图片
新闻来源:http://cdkjz.cn/article/pidesc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220