iOS UIWebview加載網頁時的圖片放大,縮小,保存到相冊,文字的拷貝

 //---縮放

    _webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

    _webView.scalesPageToFit=YES;

    _webView.multipleTouchEnabled=YES;

    _webView.userInteractionEnabled=YES;

    _webView.scrollView.scrollEnabled =YES;

    _webView.contentMode =UIViewContentModeScaleAspectFit;

    //---縮放


    //----長按圖片保存

    UILongPressGestureRecognizer* longPressed = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longPressed:)];

    longPressed.delegate =self;

    [_webViewaddGestureRecognizer:longPressed];

    //------


//長按手勢事件(保存圖片)

-(void)longPressed:(UILongPressGestureRecognizer*)recognizer{

    if (recognizer.state !=UIGestureRecognizerStateBegan) {

        return;

    }

    CGPoint touchPoint = [recognizerlocationInView:self.webView];

    

    NSString *imgURL = [NSStringstringWithFormat:@"document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y];

    NSString *urlToSave = [self.webViewstringByEvaluatingJavaScriptFromString:imgURL];

    

    if (urlToSave.length ==0) {

        return;

    }

    NSLog(@"獲取到圖片地址:%@",urlToSave);

    [selfshowImageOptionsWithUrl:urlToSave];

}

//可以識別多個手勢

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

    returnYES;

}

- (void)showImageOptionsWithUrl:(NSString *)imageUrl

{

    NSLog(@"點擊了圖片哈哈哈哈哈哈!!!!!");

    UIAlertController *alertVc = [UIAlertControlleralertControllerWithTitle:nilmessage:nilpreferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction *cancle = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *_Nonnull action) {

        

    }];

    

    UIAlertAction *camera = [UIAlertActionactionWithTitle:@"保存圖片到手機"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *action) {

        [selfsaveImageToPhoneWithImageUrl:imageUrl];

    }];

   // UIAlertAction *picture = [UIAlertAction actionWithTitle:@"分享圖片給好友" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

   //    [self sharImageTofrendsWithImageUrl:imageUrl];

   // }];

    [alertVc addAction:cancle];

    [alertVc addAction:camera];

    //[alertVc addAction:picture];

    [selfpresentViewController:alertVcanimated:YEScompletion:nil];


}

//保存圖片到手機

-(void)saveImageToPhoneWithImageUrl:(NSString *)imageUrl{

    NSLog(@"保存圖片到手機!!!!");

    __weaktypeof (self) weakSelf =self;

    [[SDWebImageManagersharedManager] downloadImageWithURL:[NSURLURLWithString:imageUrl] options:0progress:^(NSInteger receivedSize,NSInteger expectedSize) {

        

    } completed:^(UIImage *image,NSError *error, SDImageCacheType cacheType,BOOL finished, NSURL *imageURL) {

        if (!error) {

            NSLog(@"圖片下載成功!!!");

        }else{

            NSLog(@"圖片下載失敗!!!");

        }

        if (image) {

           //圖片下載完成 在這裏進行相關操作,如加到數組裏或者顯示在imageView

            [weakSelf saveImageToPhotos:image];

        }

    }];

}

- (void)saveImageToPhotos:(UIImage*)image

{

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

}

// 指定回調方法

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

{

    NSString *msg =nil ;

    if(error !=NULL){

        msg = @"圖片保存失敗了" ;

    }else{

        msg = @"圖片已經保存在您的相冊裏了" ;

    }

    UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"提示"

                                                    message:msg

                                                   delegate:self

                                          cancelButtonTitle:@"知道了"

                                          otherButtonTitles:nil];

    [alert show];

}


//分享圖片給好友

-(void)sharImageTofrendsWithImageUrl:(NSString *)imageUrl{

    NSLog(@"分享圖片給好友!!!!");

    [UMSocialSnsServicepresentSnsIconSheetView:selfappKey:kUmengAnalyticsAppKeyshareText:nilshareImage:nilshareToSnsNames:@[UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQzone,UMShareToSina,UMShareToQQ,UMShareToTencent,UMShareToRenren,UMShareToDouban]delegate:self];

}


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

    _isLoadFinish =YES;

    //網頁手勢縮放效果

    NSString *jsMeta = [NSStringstringWithFormat:@"var meta = document.createElement('meta');meta.content='width=device-width,initial-scale=1.0,minimum-scale=.5,maximum-scale=3';meta.name='viewport';document.getElementsByTagName('head')[0].appendChild(meta);"];

    [_webViewstringByEvaluatingJavaScriptFromString:jsMeta];

    //文字大小

    [_webViewstringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '90%'"];//網頁字體大小百分比即可

   // [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'black'"];//網頁字體顏色

    //[_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#2E2E2E'"]; //頁面背景色

    

    NSString *str =@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'";

    [webView stringByEvaluatingJavaScriptFromString:str];

    

    //js方法遍歷圖片添加點擊事件返回所有圖片

//    static  NSString * const jsGetImages =

//    @"function getImages(){\

//    var srcs = [];\

//    var objs = document.getElementsByTagName(\"img\");\

//    for(var i=0;i<objs.length;i++){\

//    srcs[i] = objs[i].src;\

//    };\

//    for(var i=0;i<objs.length;i++){\

//    objs[i].οnclick=function(){\

//    document.location=\"myweb:imageClick:\"+srcs;\

//    };\

//    };\

//    return objs.length;\

//    };";

    

    

    static NSString * const jsGetImages =

    @"function getImages(){\

    var srcs = [];\

    var objs = document.getElementsByTagName(\"img\");\

    for(var i=0;i<objs.length;i++){\

    srcs[i] = objs[i].src;\

    };\

    for(var i=0;i<objs.length;i++){\

    objs[i].index = i;\

    objs[i].οnclick=function(){\

    document.location=\"myweb:imageClick:\"+ srcs +'@'+this.index;\

    };\

    };\

    return objs.length;\

    };";


    

    [webView stringByEvaluatingJavaScriptFromString:jsGetImages];//注入js方法

    [webView stringByEvaluatingJavaScriptFromString:@"getImages()"];

}


覺得有用的別忘了點個贊

發佈了33 篇原創文章 · 獲贊 10 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章