How to Intercept image load requests in WebView? - iphone

Is it possible to intercept image load requests in WebView before they are actually started and modify their URLs?
For example, I have
mWebView.loadUrl(myUrl);
In onLoadResources event I can see URLs, but I can't modify them?
The thing is I am working on application that loads html content from remote location. For some reason author excluded image path and in img src he just have file name. Existing iPhone application is using this html content and I assume the content is build the way that is the best for iPhone. So, I need somehow to figure how to alter these paths. For example, if I choose to download all images first, I would need to alter path and add file:///... in front of image.jpg name.
Thanks.

you can use onLoadResource although are not only images but any resource loaded like javascript and css

Related

What's the best way to download multiple images and display multiple UIImageView?

I need to download images from a website and display them on(?) multiple UIImageView.
Maybe I'll code a php to "read" the directory and search for images, write a XML file and use it as medium. But I'm not sure if it's the best way.
Let's see the options you have to fetch images from a website:
Fetching HTML and Parsing the HTML to find the images (on the iphone). Then downloading the images.
Writing a script (maybe PHP) that writes all image links to an XML file (or JSON), and then fetch the output of your script with all the links.
If you choose option (1) you'll need NSURLConnection to fetch data asynchronously (without blocking the UI). I would also use TFHpple to parse HTML using xpath queries, see this tutorial for help. Finally to fetch the images using their URLs you can use SDWebImage, SDWebimage also provides caching so your app will not download the same image multiple times.
The bad side of using option (1) is that any change in the Website you're getting the images from will break your app and you'll need to issue an update to the app store in order to fix it.
If you choose option (2), your app will be easier to fix if the website changes, you'll just need to modify your script.
If you go with option (2) you'll probably need NSURLConnection, NSXMLParser (or a third party XML parsing library) and to download the images I would recomend SDWebImage again. I would also advise using JSON (and NSJSONSerialization) instead of XML, just beacuse I find JSON easier to parse.
Yes, it will be very good if you write some php script to get image list (list of image urls).
After getting such urls you can asynchronously download and show them in image views. Look here for such async image view implementation

Getting images by parsing constantly changing HTML

I'm in the process of developing an iOS app that retrieves images from a URL (http://m.9gag.com). It so happens that the HTML for the URL is in constant change and whenever I have a working code, the site's style changes.
Is there any way I could pull those images from the HTML without having to worry about webpage changes? There is no public API at the moment so that's sadly not an option.
I look forward to hearing some options.
Also, if the page is set so that when the user scrolls to the bottom, it loads more content, how can I get more html to load based on how far down in the HTML parsing I've got? I'm not using a webview, I just need to update the HTML I initially retrieved.
It seems that the simplest way in your case - use regular expression (for example http://[A-Za-z0-9_/\.]*\.jpg) to extract URLs and keep track of already pulled images.

Zend display image

Hi i'm using zend framework and what i want is to show an image.
My image is in a folder /uploads/photo/default.jpg.
If i move the folder to /public/... everithing works fine but I would prefere to leave there if i can because all images are profile's image so i don't want to leave them in public...
My code to show the image is
<img id="image" src=<?php echo $this->baseUrl().'/uploads/foto/default.jpg'; ?> >
And I know that it don't work but there are a way to leave my folder there changing my code?
The images need to be publicly accessible so a browser can view them. If you don't want to physically store them in the public folder you either need to make them accessible from there (by creating a symlink from public/uploads to your uploads folder), or serve them via. a PHP script, which would be able to read the data from wherever the file is located.
Serving binary data via. PHP has its own issues, as it will be slower and you'll need to ensure you send the appropriate headers. But this would allow you to restrict access, e.g. only allowing users to view their friends' profile images, if this is your goal.

How can I save a html file with external resources using AFNetworking?

I would like to save a .html webpage with AFNetworking, but would also like to save the resources (such as .css files, .js files, images etc) within the webpage so that the whole webpage can be viewed offline.
Is this possible with AFNetworking, and how would I do it? Could a short example be posted please?
Thanks!
AFNetworking is not necessary to do this. Instead, what you want to do is use an NSURLCache subclass that supports disk cacheing (such as Peter Steinberger's fork of SDURLCache). With that in place, just load up a URL using a UIWebView (this may not necessarily have to be displayed to a user), and subsequent loads should use that local cache.
At the very least, do not waste your time trying to write something on your own to download assets on a webpage. This process requires a web browser (which UIWebView qualifies as) to determine everything needed to load.

Using local resources in an iPhone webview

Suppose an app has a webview that uses JQuery for example.
The server delivers the page with appropriate links to load JQuery, and the view works fine, but I would like it to not need to download JQuery (yes it may be cached, but it won't be the first time the app runs and I would rather not count on it)
So I can include the JQuery files with the app, but then how should I embed the links?
The server certainly doesn't know the bundle path.
I thought I could load the url into a string and then replace the JQuery paths with the local paths before displaying in the webview.
Is there an easier way?
You can use relative paths for jQuery and set the baseURL to the bundle path. However that requires that all resources are in the bundle directory (except those which are referenced by an absolute path).