新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

ios开发怎么获取网页上的全部图片-创新互联

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

新郑网站建设公司创新互联公司,新郑网站设计制作,有大型网站制作公司丰富经验。已为新郑上千余家提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的新郑做网站的公司定做!

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

1、首先到如第三方库 TFHpple

2、

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

-(void)dowmLoadPhoto{

  NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:path]];

  NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

  if (response == nil){

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"警告!" message:@"无法连接到该网站!" delegate:nil cancelButtonTitle:@"确定" 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 = [[TFHpple alloc] initWithHTMLData:data];

  //在页面中查找img标签

  NSArray *p_w_picpaths = [doc searchWithXPathQuery:@"//img"];

  return p_w_picpaths;

}

//下载图片的方法

- (NSMutableArray*)downLoadPicture:(NSArray *)p_w_picpaths

{

  //创建存放UIImage的数组

  _downloadImages = [[NSMutableArray alloc] init];

  _p_w_picpathViewArr =[[NSMutableArray alloc]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_picpaths objectAtIndex: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 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];

  [_wv addGestureRecognizer:singleTap];

  singleTap.delegate = self;

  singleTap.cancelsTouchesInView = NO;

}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

  return YES;

}

//获取点击的图片

-(void)handleSingleTap:(UITapGestureRecognizer *)sender

{

  CGPoint pt = [sender locationInView:_wv];

  NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", pt.x, pt.y];

  NSString *urlToSave = [_wv stringByEvaluatingJavaScriptFromString:imgURL];

  if([urlToSave hasPrefix:@"http"])

  {

    int index = [_downloadImages indexOfObject:urlToSave];

    int count = _downloadImages.count;

  // 1.封装图片数据

  NSMutableArray *photos = [[NSMutableArray alloc]init];

  NSMutableArray *_viewArr =[[NSMutableArray alloc]init];

    for (int i = 0; i

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

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

        [photos addObject:photo];

    }

}

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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享名称:ios开发怎么获取网页上的全部图片-创新互联
分享地址:http://www.wjwzjz.com/article/dsspdh.html
在线咨询
服务热线
服务热线:028-86922220
TOP