UIWebView長(zhǎng)按保存圖片js交互

demo地址:https://github.com/daijuqing/UIWebViewCopyImage

#import "ViewController.h"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\";};";@interface ViewController (){

UIWebView *_webView;

NSTimer *_timer;

NSInteger _gesState;

NSString *_imgURL;

}

@end

@implementation ViewController

typedef NS_ENUM(NSInteger, GESTURE_STATE) { GESTURE_STATE_START, GESTURE_STATE_MOVE, GESTURE_STATE_END};

- (void)viewDidLoad {

[super viewDidLoad];

[self loadWebView];

}

- (void)loadWebView{

_webView = [[UIWebView alloc]initWithFrame:self.view.bounds];

[self.view addSubview:_webView];

_webView.delegate? =self;

NSURL *url = [NSURL URLWithString:@"http://m.baidu.com"];

NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];

[_webView loadRequest:request];

}

- (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"])

{

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

{

/*

@需延時(shí)判斷是否響應(yīng)頁面內(nèi)的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 = [_webView 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動(dòng)作是滑動(dòng),則取消hanleLongTouch動(dòng)作**//

_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 YES;

}

- (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 = [_webView stringByEvaluatingJavaScriptFromString:_imgURL];

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

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

UIImage* image = [UIImage imageWithData:data];

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

}

}

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

{

if (error){

NSLog(@"Error");

//? ? ? ? [self showAlert:SNS_IMAGE_HINT_SAVE_FAILE];

}else {

NSLog(@"OK");

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提醒" message:@"保存成功!" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];

[alertView show];

}

}

-(void)webViewDidFinishLoad:(UIWebView *)webView {

[_webView stringByEvaluatingJavaScriptFromString:kTouchJavaScriptString];

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容