我又在苦苦挣扎,这次得到一个警报视图以显示正确的错误信息,我有一个 2 指点击手势,当您单击例如带有 2 个手指的谷歌图像(全尺寸)图像时,它会保存图像并显示正确的警报“图像已保存”,但您可以用 2 根手指单击任何地方,它会显示“图像已保存”,即使它没有,因为没有要保存的图像。因此,如果有人可以在他们对图像以外的其他东西做手势时帮助我显示消息这是完整的代码问题可能在其他地方,因为 *tagname 变量也未使用提前谢谢
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
//DoubleTap Gesture
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
doubleTap.numberOfTouchesRequired = 2;
[self.myWebView addGestureRecognizer:doubleTap];
}
//Double Tap Method
-(void) doubleTap :(UITapGestureRecognizer*) sender {
int scrollPositionY = [[self.myWebView stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] intValue];
int scrollPositionX = [[self.myWebView stringByEvaluatingJavaScriptFromString:@"window.pageXOffset"] intValue];
int displayWidth = [[self.myWebView stringByEvaluatingJavaScriptFromString:@"window.outerWidth"] intValue];
CGFloat scale = myWebView.frame.size.width / displayWidth;
CGPoint pt = [sender locationInView:self.myWebView];
pt.x *= scale;
pt.y *= scale;
pt.x += scrollPositionX;
pt.y += scrollPositionY;
NSString *js = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).tagName", pt.x, pt.y];
NSString * tagName = [self.myWebView stringByEvaluatingJavaScriptFromString:js];
NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", startPoint.x, startPoint.y];
NSString *urlToSave = [self.myWebView stringByEvaluatingJavaScriptFromString:imgURL];
NSURL *url = [NSURL URLWithString:urlToSave];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
UIImageWriteToSavedPhotosAlbum(image,self,@selector(savedPhotoImage:didFinishSavingWithError:contextInfo:),nil);
}
//Alert for double tap save photo
- (void) savedPhotoImage:(UIImage *)image
didFinishSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo
{
UIAlertView *alert;
// Unable to save the image
if (error)
alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
else // All is well
alert = [[UIAlertView alloc] initWithTitle:@"Success"
message:@"Image saved to Photo Album."
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
}