[掌眼]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 = [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 长按图片保存到手机

时间: 2024-10-04 08:43:00

[掌眼]IOS UIWebView Long press save image 长按图片保存到手机的相关文章

[掌眼]Android WebView Long Press长按保存图片到手机

具体效果可见"掌眼"掌拍古玩江湖:http://bbs.guwanch.com private String imgurl = ""; /*** * 功能:长按图片保存到手机 */ @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); Me

[掌眼]iOS / Android / java / node.js 通用的 AES256 加解密算法

example.m NSString *text = @"text"; NSString *key32 = @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding]; NSString *encryptedData = [[data AES256EncryptWithKey:key32] base64EncodedStringWi

怎么将图片保存在手机照片瀑布流主目录下 -----ios

UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); 这个方法可以将保存图片保存在瀑布流主目录下 这个方法检验有没有保存成功 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)

[掌眼]UIWebView 显示正在加载网页

具体效果可见"掌眼"掌拍古玩江湖:http://bbs.guwanch.com - (void)webViewDidStartLoad:(UIWebView *)webView { [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; } - (void)webViewDidFinishLoad:(UIWebView *)webView { [UIApplication sharedApplic

ios UIWebView 加载网页、文件、 html

UIWebView  是用来加载加载网页数据的一个框.UIWebView可以用来加载pdf word doc 等等文件 生成webview 有两种方法,1.通过storyboard 拖拽 2.通过alloc init 来初始化 创建webview,下列文本中 _webView.dataDetectorTypes = UIDataDetectorTypeAll; 是识别webview中的类型,例如 当webview中有电话号码,点击号码就能直接打电话 - (UIWebView *)webView

iOS UIWebView 加载https站点出现NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL,

今天在加载https站点的时候遇到如下的错误问题.所以对自己之前写的iOS内嵌webview做了一些修改,可以让它加载http站点也可以让它加载https站点. 下面是我加载https站点的时候出现的错误. error:     NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813) HTTPS 超文本传输安全协议(缩写:HTTPS,英语:Hypertext Transfer Protoc

[掌眼]微信支付测试返回:access_control:not_allow或system:access_denied

转自:http://mp.weixin.qq.com/qa/index.php?qa=11634&qa_1=%E6%94%AF%E4%BB%98%E8%BF%94%E5%9B%9E%EF%BC%9Aaccess_control-not_allow&show=16550 感谢:乐游旅游 比如我的测试js支付的页面是 http://10.10.x.y/test/wxpay .首先要将"商户功能"->支付测试 下的"支付测试目录"改成  http:/

iOS UIWebView 通过 cookie 完成自动登录验证

一些说明: 通过UIWebView登录后,会自动得到web服务器设置的cookie包括服务器中的seesionid. cookie不会自动保存在app里面,需要通过设置才能在下次启动app时获取. 自动登录,需要设置header,才能将cookie带给web服务器. 自动登录,需要web服务器端进行cookie验证方可登录. 实现流程: 1. 做好Sign in页面后,通过get或post递交表单给web服务器,可以通过下面的代码遍历得到的cookie. NSHTTPCookieStorage

iOS UIWebView截获html并修改便签内容

UIWebView使用中经常遇到用JS来处理的事情,今天又遇到了搜了搜,找了这篇文章 感觉不错 珍藏一下. 原文地址 http://jiapumin.iteye.com/blog/1558345    感谢原作者jiapumin 需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果: 方法:通过js截获UIWebView中的html,然后修改html标签内容: 实例代码: 服务器端html <html><head> <meta h