ユーザーが UIWebView で画像を押したときを検出し、UIActivityViewController を起動して画像やその他のものを保存するジェスチャ認識機能 (2 本の指でタップ) を追加しました。私の問題は、画像を押すと(画像サイズの大きさに応じて)ラグが発生し、ユーザーが画像をダブルタップしたときにアクティビティインジケーターを表示したいということです。画像を保存する準備ができているなど...問題はここにあると思うことを追加するかもしれません 'imageToBeSaved = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];' そのコードを取り出した後、遅れはなくなりました。これが私のコードです:
// adding gesture recognizer to "website" (which is a "UIWebView")
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
doubleTap.numberOfTouchesRequired = 2;
[website addGestureRecognizer:doubleTap];
// actions on gesture detect and call actionsOnImage for options
-(void) doubleTap :(UITapGestureRecognizer*) sender {
[loadingImageActivityIndicator startAnimating];
int scrollPositionY = [[self.website stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] intValue];
int scrollPositionX = [[self.website stringByEvaluatingJavaScriptFromString:@"window.pageXOffset"] intValue];
int displayWidth = [[self.website stringByEvaluatingJavaScriptFromString:@"window.outerWidth"] intValue];
CGFloat scale = website.frame.size.width / displayWidth;
CGPoint pt = [sender locationInView:self.website];
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.website stringByEvaluatingJavaScriptFromString:js];
if ([tagName compare:@"IMG" options:NSCaseInsensitiveSearch]==NSOrderedSame) {
NSLog(@"tagName was IMG");
NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", pt.x, pt.y];
NSString *urlToSave = [self.website stringByEvaluatingJavaScriptFromString:imgURL];
NSURL *url = [NSURL URLWithString:urlToSave];
imageToBeSaved = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
[website addSubview:loadingImageActivityIndicator];
[self actionsOnImage]; // self made function with sharing capabilities (see below)
}
}
- (void) actionsOnImage {
[loadingImageActivityIndicator stopAnimating];
NSArray* dataToShare = [[NSArray alloc] initWithObjects:imageToBeSaved, nil];
UIActivityViewController* activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:dataToShare
applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:^{}];
}