WebViewの長さは識別の二次元コードを押して、クリックして大図を見ます.
8767 ワード
ローカル識別二次元コード
文末にDemoの住所があります.とても簡単です.字を見たくないなら、直接デモを見られます.
CIDetecor
1,まずローカルの二次元コードを識別します.webViewは基本的にjs文の問題だけです.QRCodeDetectorクラスを作成します.
WebView追加長押しジェスチャー
プロジェクトの需要のため、変更すべきところが多すぎて、直接にユーザー定義のWebViewに書いて、直接webViewを交換すればいいです.会社の必要なwebはあまり複雑な機能がないので、直接にユーザー定義のwebViewに書いてもいいです.
カスタムwebView追加長押しジェスチャー:
YNQRCodeWebView
文末にDemoの住所があります.とても簡単です.字を見たくないなら、直接デモを見られます.
CIDetecor
1,まずローカルの二次元コードを識別します.webViewは基本的にjs文の問題だけです.QRCodeDetectorクラスを作成します.
#import "YNQRCodeDetector.h"
@implementation YNQRCodeDetector
+ (CIQRCodeFeature *)yn_detectQRCodeWithImage:(UIImage *)image {
// 1.
CIContext *context = [[CIContext alloc] init];
// 2.
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:context options:@{CIDetectorAccuracy: CIDetectorAccuracyLow}];
// 3.
CIImage *imageCI = [[CIImage alloc] initWithImage:image];
NSArray *features = [detector featuresInImage:imageCI];
CIQRCodeFeature *codeF = (CIQRCodeFeature *)features.firstObject;
return codeF;
}
@end
2,ローカルイメージビューにジェスチャーを追加し、二次元コードの識別のリンクを処理し、二次元コードの表示保存と識別があり、保存のみを表示しません.- (void)imageLongPress {
UIAlertController *ac = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
CIQRCodeFeature *codeF = [YNQRCodeDetector yn_detectQRCodeWithImage:self.imageView.image];
[ac addAction:[UIAlertAction actionWithTitle:@" " style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[ac addAction:[UIAlertAction actionWithTitle:@" " style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self savePicture];
}]];
if (codeF.messageString) {
[ac addAction:[UIAlertAction actionWithTitle:@" " style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//
SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:codeF.messageString]];
[self presentViewController:safariVC animated:YES completion:nil];
}]];
}
[self presentViewController:ac animated:YES completion:nil];
}
3、写真を保存するのは何も言いません.iOS 10以降はマニュアルでアルバムの権限を開いてください.- (void)savePicture {
if (self.imageView.image == nil) {
// [SVProgressHUD showErrorWithStatus:@" "];
} else {
UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
}
- (void)image:(UIImage *)image
didFinishSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo {
if (error) {
// [SVProgressHUD showErrorWithStatus:@" "];
} else {
// [SVProgressHUD showSuccessWithStatus:@" "];
}
}
WebViewのいくつかの処理WebView追加長押しジェスチャー
プロジェクトの需要のため、変更すべきところが多すぎて、直接にユーザー定義のWebViewに書いて、直接webViewを交換すればいいです.会社の必要なwebはあまり複雑な機能がないので、直接にユーザー定義のwebViewに書いてもいいです.
カスタムwebView追加長押しジェスチャー:
- (instancetype)init {
self = [super init];
if (self) {
[self basicConfigure];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self basicConfigure];
}
return self;
}
- (void)basicConfigure {
self.delegate = self;
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressWebPic:)];
longPress.delegate = self;
[self addGestureRecognizer:longPress];
}
- (void)longPressWebPic:(UILongPressGestureRecognizer *)recognizer {
if (recognizer.state != UIGestureRecognizerStateBegan) {
return;
}
// ,
CGPoint touchPoint = [recognizer locationInView:self];
NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y];
NSString *urlToSave = [self stringByEvaluatingJavaScriptFromString:imgURL];
if (urlToSave.length == 0) {
return;
}
// ENLog(@"%@", urlToSave);
[self imageWithUrl:urlToSave];
}
画像をダウンロード// demo , sdImageDownloader
- (void)imageWithUrl:(NSString *)imageUrl {
// , , alertController
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url = [NSURL URLWithString:imageUrl];
NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue new]];
NSURLRequest *imgRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30.0];
NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:imgRequest completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
return ;
}
NSData *imageData = [NSData dataWithContentsOfURL:location];
// UI
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:imageData];
UIAlertController *ac = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
CIQRCodeFeature *codeF = [YNQRCodeDetector yn_detectQRCodeWithImage:image];
[ac addAction:[UIAlertAction actionWithTitle:@" " style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[ac addAction:[UIAlertAction actionWithTitle:@" " style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self savePicture:image];
}]];
if (codeF.messageString) {
[ac addAction:[UIAlertAction actionWithTitle:@" " style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:codeF.messageString]];
[[self currentViewController] presentViewController:safariVC animated:YES completion:nil];
}]];
}
// webView
[[self currentViewController] presentViewController:ac animated:YES completion:nil];
});
}];
[task resume];
});
}
最上位のコントローラを取得- (UIViewController *)currentViewController {
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
UIViewController *vc = keyWindow.rootViewController;
while (vc.presentedViewController) {
vc = vc.presentedViewController;
if ([vc isKindOfClass:[UINavigationController class]]) {
vc = [(UINavigationController *)vc visibleViewController];
} else if ([vc isKindOfClass:[UITabBarController class]]) {
vc = [(UITabBarController *)vc selectedViewController];
}
}
return vc;
}
- (UINavigationController *)currentNavigationController {
return [self currentViewController].navigationController;
}
WebViewの取扱代理方法(js処理)- (void)webViewDidFinishLoad:(UIWebView *)webView {
//
[self stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
NSString * jsCallBack = @"window.getSelection().removeAllRanges();";
[webView stringByEvaluatingJavaScriptFromString:jsCallBack];
//js
static NSString * const jsGetImages =
@"function getImages(){\
var objs = document.getElementsByTagName(\"img\");\
for(var i=0;i
アドレスYNQRCodeWebView