温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-07-30 18:28:45 来源:亿速云 阅读:173 作者:chen 栏目:移动开发

这篇文章主要介绍“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<count-1; i++) {

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

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

                [photos addObject:photo];

        }

}

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI