Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the image from uiwebview in ios

here i loaded the content as well as the images in the webview, i want to take the image from the webview and load it into the imageview.

the url of the image is received but the image is not loaded it in to the imageview

here i use the code for taking the image from webview is

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    //Touch gestures below top bar should not make the page turn.
    //EDITED Check for only Tap here instead.
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
        CGPoint touchPoint = [touch locationInView:self.view];

        NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
        bool pageFlag = [userDefaults boolForKey:@"pageDirectionRTLFlag"];
        NSLog(@"pageFlag tapbtnRight %d", pageFlag);

        if(self.interfaceOrientation==UIInterfaceOrientationPortrait||self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
  NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y];
            NSString *urlToSave = [webView stringByEvaluatingJavaScriptFromString:imgURL];
            NSLog(@"urlToSave :%@",urlToSave);
}
}
like image 511
dineshprasanna Avatar asked Mar 14 '12 13:03

dineshprasanna


People also ask

How do I display an image in Xcode?

Drag and drop image onto Xcode's assets catalog. Or, click on a plus button at the very bottom of the Assets navigator view and then select “New Image Set”. After that, drag and drop an image into the newly create Image Set, placing it at appropriate 1x, 2x or 3x slot.

What is iOS WKWebView?

Overview. A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.

Which browser is used in iOS WebView?

On iOS the web rendering engine is always WebKit, the same one that powers Safari...and Chrome.


1 Answers

Check this out.

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
NSLog(@"TAPPED");
//Touch gestures below top bar should not make the page turn.
//EDITED Check for only Tap here instead.
if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
    CGPoint touchPoint = [touch locationInView:self.view];

    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    bool pageFlag = [userDefaults boolForKey:@"pageDirectionRTLFlag"];
    NSLog(@"pageFlag tapbtnRight %d", pageFlag);

    if(self.interfaceOrientation==UIInterfaceOrientationPortrait||self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
        NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y];
        NSString *urlToSave = [webV stringByEvaluatingJavaScriptFromString:imgURL];
        NSLog(@"urlToSave :%@",urlToSave);
        NSURL * imageURL = [NSURL URLWithString:urlToSave];
        NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
        UIImage * image = [UIImage imageWithData:imageData];
        imgView.image = image;//imgView is the reference of UIImageView
    }
  }
return YES;
}

Sample code: http://dl.dropbox.com/u/51367042/ImageFromWebView.zip

like image 129
Prasad De Zoysa Avatar answered Oct 21 '22 19:10

Prasad De Zoysa