【问题标题】:Save image from UIWebView to camera roll with gesture使用手势将图像从 UIWebView 保存到相机胶卷
【发布时间】:2012-12-07 22:09:33
【问题描述】:

您好,我在保存图像时有点卡住,我已经构建了一个手势并按照教程进行操作,但在实际保存图像的最后部分有点卡住。 任何帮助将不胜感激 这是教程来源

http://bees4honey.com/blog/tutorial/how-to-save-an-image-from-uiwebview/

提前致谢

    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
doubleTap.numberOfTouchesRequired = 2;
[self.myWebView addGestureRecognizer:doubleTap];


}

-(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];


/// Stuck at this point to actually get the file

}

【问题讨论】:

    标签: ios image uiwebview save


    【解决方案1】:
    NSURL *url = [NSURL URLWithString:urlToSave];
    
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
    
    UIImageWriteToSavedPhotosAlbum(image, <#id completionTarget#>, <#SEL completionSelector#>, <#void *contextInfo#>)
    

    您可以将 nil 传递给所有三个参数。这样你就可以打电话了

    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    

    【讨论】:

    • 感谢您的回复,这与我正在寻找的内容非常接近,但我收到错误 Sending NSString strong to parameter of NSURL 的错误,当我尝试手势时,我在调试中得到了这个 __NSCFString isFileURL , 如果您有其他想法,我很想尝试一下,或者我之前犯了一个错误,再次感谢
    • 现在试试这个。我忘了用你拥有的字符串创建一个 url。这应该有效。我对上述答案进行了修改。
    • 非常感谢,非常好用,感谢您的帮助
    • 非常感谢!这是最直接的,它只是我找到的解决方案
    猜你喜欢
    • 1970-01-01
    • 2015-11-18
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    相关资源
    最近更新 更多