iOS UIWebView加载的h5,下载网页上的图片

第一步:在加载完成的代理方法里注入js代码

  

-(void)webViewDidFinishLoad:(UIWebView *)webView

{

    // 注入js代码

    static NSString* const kTouchJavaScriptString=

    @"document.ontouchstart=function(event){

    x=event.targetTouches[0].clientX;

    y=event.targetTouches[0].clientY;

    document.location="myweb:touch:start:"+x+":"+y;};

    document.ontouchmove=function(event){

    x=event.targetTouches[0].clientX;

    y=event.targetTouches[0].clientY;

    document.location="myweb:touch:move:"+x+":"+y;};

    document.ontouchcancel=function(event){

    document.location="myweb:touch:cancel";};

    document.ontouchend=function(event){

    document.location="myweb:touch:end";};";

    

    [self.myWebView stringByEvaluatingJavaScriptFromString:kTouchJavaScriptString];

 }

第二步:在点击网页开始请求的代理方法里做出判断,并添加处理弹出提示与实现下载功能

 

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    NSString *requestString = [[request URL] absoluteString];

    NSArray *components = [requestString componentsSeparatedByString:@":"];

    if ([components count] > 1 && [(NSString *)[components objectAtIndex:0]

                                   isEqualToString:@"myweb"]) {

        if([(NSString *)[components objectAtIndex:1] isEqualToString:@"touch"])

        {

            //NSLog(@"you are touching!");

            //NSTimeInterval delaytime = Delaytime;

            if ([(NSString *)[components objectAtIndex:2] isEqualToString:@"start"])

            {

                /*

                 @需延时判断是否响应页面内的js...

                 */

                isTouch = YES;

                NSLog(@"touch start!");

                

                float ptX = [[components objectAtIndex:3]floatValue];

                float ptY = [[components objectAtIndex:4]floatValue];

                NSLog(@"touch point (%f, %f)", ptX, ptY);

                

                NSString *js = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).tagName", ptX, ptY];

                NSString * tagName = [self.myWebView stringByEvaluatingJavaScriptFromString:js];

                _imgURL = nil;

                if ([tagName isEqualToString:@"IMG"]) {

                    _imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", ptX, ptY];

                }

                if (_imgURL) {

                    _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(handleLongTouch) userInfo:nil repeats:NO];

                }

            }

            else if ([(NSString *)[components objectAtIndex:2] isEqualToString:@"move"])

            {

                //**如果touch动作是滑动,则取消hanleLongTouch动作**//

                [_timer invalidate];

                _timer = nil;

                isTouch = NO;

                NSLog(@"you are move");

            }

        }

        else if ([(NSString*)[components objectAtIndex:2]isEqualToString:@"end"]) {

            [_timer invalidate];

            _timer = nil;

            isTouch = NO;

            NSLog(@"touch end");

        }

    

            return NO;

        }

    return YES;

}

 

- (void)handleLongTouch {

    NSLog(@"%@", _imgURL);

    if (_imgURL && isTouch) {

        UIActionSheet* sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"保存图片", nil];

        sheet.cancelButtonIndex = sheet.numberOfButtons - 1;

        [sheet showInView:self.view];

    }

}

 

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (actionSheet.numberOfButtons - 1 == buttonIndex) {

        return;

    }

    NSString* title = [actionSheet buttonTitleAtIndex:buttonIndex];

    if ([title isEqualToString:@"保存图片"]) {

        if (_imgURL) {

            NSLog(@"imgurl = %@", _imgURL);

        }

        _hud  = [MBProgressHUD showHUDAddedTo:self.view animated:YES];

        _hud.mode = MBProgressHUDModeIndeterminate;

        _hud.delegate = self;

        _hud.labelText = @"正在下载";

        

        NSString *urlToSave = [self.myWebView stringByEvaluatingJavaScriptFromString:_imgURL];

        NSLog(@"image url=%@", urlToSave);

        

        NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlToSave]];

                                                      UIImage* image = [UIImage imageWithData:data];

                                                      

                                                      //UIImageWriteToSavedPhotosAlbum(image, nil, nil,nil);

                                                      UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

                                                      }

    }

                                                      

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo

        {

            

            _hud.mode = MBProgressHUDModeText;

 

            if (error){

                NSLog(@"Error");

                _hud.labelText = @"下载错误,请重试。";

               }else {

                NSLog(@"OK");

                _hud.labelText = @"成功保存到相册";

                [_hud hide:YES afterDelay:0.5f];

         

            }

        }

 

 

 

原文地址:https://www.cnblogs.com/XHShare/p/5240914.html