首页 > 代码库 > [掌眼]IOS UIWebView Long press save image 长按图片保存到手机
[掌眼]IOS UIWebView Long press save image 长按图片保存到手机
具体效果可下载“掌眼”古玩江湖进行测试:http://bbs.guwanch.com
ViewController.h
@interface ViewController : CDVViewController<UIActionSheetDelegate>{ NSTimer *_timer; // 用于UIWebView保存图片 int _gesState; // 用于UIWebView保存图片 NSString *_imgURL; // 用于UIWebView保存图片 } 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\";};"; // 用于UIWebView保存图片 enum { GESTURE_STATE_NONE = 0, GESTURE_STATE_START = 1, GESTURE_STATE_MOVE = 2, GESTURE_STATE_END = 4, GESTURE_STATE_ACTION = (GESTURE_STATE_START | GESTURE_STATE_END), };
ViewController.m
// 网页加载完成时触发 #pragma mark UIWebDelegate implementation - (void)webViewDidFinishLoad:(UIWebView*)theWebView { // Black base color for background matches the native apps theWebView.backgroundColor = [UIColor blackColor]; NSString *title = [theWebView stringByEvaluatingJavaScriptFromString:@"document.title"]; self.navigationItem.title = [self isBlank:title]?@"掌眼":title; // 当iOS版本大于7时,向下移动20dp if (!IOS7) { } // 防止内存泄漏 [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"]; // 响应touch事件,以及获得点击的坐标位置,用于保存图片 [theWebView stringByEvaluatingJavaScriptFromString:kTouchJavaScriptString]; return [super webViewDidFinishLoad:theWebView]; } // 功能:UIWebView响应长按事件 -(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... */ _gesState = GESTURE_STATE_START; 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.webView stringByEvaluatingJavaScriptFromString:js]; _imgURL = nil; if ([tagName isEqualToString:@"IMG"]) { _imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", ptX, ptY]; } if (_imgURL) { _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(handleLongTouch) userInfo:nil repeats:NO]; } } else if ([(NSString *)[components objectAtIndex:2] isEqualToString:@"move"]) { //**如果touch动作是滑动,则取消hanleLongTouch动作**// _gesState = GESTURE_STATE_MOVE; NSLog(@"you are move"); } } else if ([(NSString*)[components objectAtIndex:2]isEqualToString:@"end"]) { [_timer invalidate]; _timer = nil; _gesState = GESTURE_STATE_END; NSLog(@"touch end"); } return NO; } return [super webView:webView shouldStartLoadWithRequest:_request navigationType:navigationType]; } // 功能:如果点击的是图片,并且按住的时间超过1s,执行handleLongTouch函数,处理图片的保存操作。 - (void)handleLongTouch { NSLog(@"%@", _imgURL); if (_imgURL && _gesState == GESTURE_STATE_START) { UIActionSheet* sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"保存到手机", nil]; sheet.cancelButtonIndex = sheet.numberOfButtons - 1; [sheet showInView:[UIApplication sharedApplication].keyWindow]; } } // 功能:保存图片到手机 - (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); } NSString *urlToSave = [self.webView stringByEvaluatingJavaScriptFromString:_imgURL]; NSLog(@"image url = %@", urlToSave); NSData* data =http://www.mamicode.com/ [NSData dataWithContentsOfURL:[NSURL URLWithString:urlToSave]]; UIImage* image = [UIImage imageWithData:data]; //UIImageWriteToSavedPhotosAlbum(image, nil, nil,nil); NSLog(@"UIImageWriteToSavedPhotosAlbum = %@", urlToSave); UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); } } // 功能:显示对话框 -(void)showAlert:(NSString *)msg { NSLog(@"showAlert = %@", msg); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil]; [alert show]; } // 功能:显示图片保存结果 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo { if (error){ NSLog(@"Error"); [self showAlert:@"保存失败..."]; }else { NSLog(@"OK"); [self showAlert:@"保存成功!"]; } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。