fuzionpro |
Detecting Images in UIWebView iOS Posted: 04 Feb 2014 07:06 AM PST Now a days, In most of the iOS applications showing multimedia and rich contents are very common and also its necessary to keep the user engaging when displaying the contents. Displaying contents alone doesn’t make the user happy but how we are showing is where we can capture the real user happiness and get more hooked to the app. In this tutorial, We are going to discuss on Detecting Images in UIWebview and show them separately and interactively like the example shown below. Steps for detecting images in UIWebViewStep 1
Step 2
//Assign the Url to loaded NSString *urladdress = @"http://www.apple.com/iphone/"; //Add the url to the NSURL NSURL *url = [NSURL URLWithString:urladdress]; //Request the Url to load NSURLRequest *req = [NSURLRequest requestWithURL:url]; [_webView loadRequest:req]; Step 3
UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTapGesture:)]; //Set the no. of taps doubleTapGesture.numberOfTapsRequired = 2; //Set the delegate doubleTapGesture.delegate = self; //Add the gesture to the webview [self.webView addGestureRecognizer:doubleTapGesture];
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; }
var names = []; var a = document.getElementsByTagName(\"IMG\"); for (var i=0, len=a.length; i<len; i++) { names.push(document.images[i].src); } String(names);
- (void)doubleTapGesture:(UITapGestureRecognizer *)sender { NSString *script = @"var names = []; var a = document.getElementsByTagName(\"IMG\");for (var i=0, len=a.length; i<len; i++){names.push(document.images[i].src);}String(names);"; }
NSString *urls = [self.webView stringByEvaluatingJavaScriptFromString:script]; Finally, we got all the image sources in the string variable “urls” and how we are going to make use of the urls to display image sources will be seen in the next step Step 4
- (void)doubleTapGesture:(UITapGestureRecognizer *)sender { NSString *script = @"var names = []; var a = document.getElementsByTagName(\"IMG\");for (var i=0, len=a.length; i<len; i++){names.push(document.images[i].src);}String(names);"; NSString *urls = [self.webView stringByEvaluatingJavaScriptFromString:script]; //ViewController to display images ContentPopViewController * contentPopVC = [[ContentPopViewController alloc] initWithNibName:@"ContentPopViewController_iPhone" bundle:nil]; [contentPopVC setModalPresentationStyle:UIModalPresentationPageSheet]; [contentPopVC setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; contentPopVC.imageIndentifier = @"gallery"; contentPopVC.imagesArray = (NSMutableArray *)[urls componentsSeparatedByString:@","]; [self presentViewController:contentPopVC animated:YES completion:nil]; } Step 5 Now we are moving into the “ContentPopViewController” where we are showing the images that we got from the WebView. In the viewDidLoad call the method [self displayImage:imagesArray]; to load the image source - (void)displayImage:(NSMutableArray *)imageArray { NSMutableString *html = [NSMutableString string]; for (NSString *str in imageArray) { [html appendFormat:@"<div style=\"text-align:center;margin:10px;hspace=\"20\"><img src='%@' ></div>", str]; } _webContent.contentMode = UIViewContentModeScaleAspectFit; [_webContent loadHTMLString:html baseURL:nil]; } Step 6
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; Finally, output is show in the image below That’s it from this tutorial. Do checkout this space for more amazing and exciting tutorials to come !!! The post Detecting Images in UIWebView iOS appeared first on Fuzionpro. |
You are subscribed to email updates from Fuzionpro To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |