具体效果可下载“掌眼”古玩江湖进行测试: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 = [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:@"保存成功!"];
}
}
[掌眼]IOS UIWebView Long press save image 长按图片保存到手机