caching the webcontent ( images ) on the iPhone - iphone

I have a question concerning the caching of webcontent.
I've created a UIWebview component
Code:
NSString *urlAddress = #"http://192.168.55.101/~test/mobile/iphone/ads/v0.1/";
//URL OBJECT footer
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320 , 100)];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[aWebView loadRequest:requestObj];
This shows a picture in my iPhone.
I've tried looking into cache attributes and functions (for instance NSURLRequestReturnCacheDataElseLoad ) to see which options I have , but what's the best way to cache the image appearing in this screen.
Or how do I use NSURLRequestReturnCacheDataElseLoad?

There is a good sample code:
Apple sample code
Description:
CacheInfo allows you to choose a URL, create a connection, load the resource asynchronously, and observe results such as the data size, load time, and cache usage. You can adjust the sizes of the shared memory and disk caches and observe how caching is affected during the load.

Related

How to set cache expiration time when using UIWebView and NSURLRequest?

I'm looking for the most simple default solution. Currently, I have one about view controller where some about info from some url is shown. I need to cache that for offline usage, and cache should be updated after some time, for example after a week. Currently , I'm using NSURLRequestReturnCacheDataElseLoad cache policy but don't know how to set cache expiration and cache update time:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSString *urlString = [NSString stringWithFormat:#"%#%#", kServiceBaseUrl, #"docs/about_en.html"];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
[self.webView loadRequest:request];
}
I have read several posts where manual cache handling (get NSData, save, check and load) is suggested. But maybe there's more straightforward and simple solution?
You can manually clear the cache like this:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
As for expiration and handling options, take a look at the NSURLRequest Class Reference dealing with cache here.
You can get a detailed explanation of Understanding Cache Access from the Apple URL Loading System Programming Guide here.

How to load large size PDF scroll view in iOS?

I have pdf of size 53MB when i try to load only one page of PDF in scrollview i got memory warning and then application crash.
Display the PDF in a UIWebView:
NSString *PDFPath = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"pdf"];
NSURL *URL = [NSURL fileURLWithPath:PDFPath];
NSURLRequest *URLRequest = [NSURLRequest requestWithURL:URL];
[webView loadRequest:URLRequest];
The WebView will load pages into memory as they appear on screen rather than all at once.
This is a duplicate of What is the best way to view large PDFs on iOS?
The accepted answer is to use a UIWebView as indicated above.
You should take a look at the vfr/Reader, which is a PDF Reader Core for iOS.
UIWebView is not very good in handling big files out of the box.

How to Load a WebView pre-zoomed in? (In Xcode / Objective C)

I am loading a UIWebview of a hosted .jpg. It's actually a schedule, so it is a rather large image. Instead of having users have to zoom in right away, I would like to load the web view already zoomed in. Although I still need the user to be able to zoom in and out, and scroll. Basically I am just looking for an "initial" zoom. How would I accomplish this? Just FYI I put the method below I am using to load the image...Thanks!
// Webview code
NSString *urlAddress = #"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webview loadRequest:requestObj];
In iOS 5 you can access the UIWebView's scrollView property and set the zoom level on it after the page has loaded. That would likely work as you wanted. To get the same thing on pre-iOS 5 you'd probably want to go through the UIWebView's subviews until you find a UIScrollView and do the same thing on that.
I don't think there's any other way to programatically zoom it. Unless you can do it with Javascript and execute some using UIWebView's stringByEvaluatingJavaScriptFromString: method.

Why UIWebView Eating so many Memory?

I'm using a UIWebView to load a pure Text HTML page for my iPad app. The size of the HTMP page is only 40KB. But when I use the instrument to monitor the memory use for loading the UIWebView, I found down it consumes like 20MB memory, if I scroll the web view, the memory is even getting higher. Finally I get a level 1 memory warning.
Could anyone help me with this? How could I reduce the memory for this? (I need to use the HTML to show the text here).
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"SPC"];
NSURL *url = [NSURL fileURLWithPath:htmlPath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
htmlPath
url
request
release all of them after this line
[webView loadRequest:request];
then release webview in dealloc and use webview as ivar

Loading image from url problem iphone

I'm loading images from url into webviews but that's showing me images which are cut down. I tried doing sizeToFit, but that shows a very small image cornered at left in my webview as the webage is large and image at its upper left corner.
EDIT:
This' how I'm loading images in webview. Here, expanded_photo is webview.
NSString *urlAddress = [NSString stringWithFormat:#"%#",photo_url];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[expanded_photo loadRequest:requestObj];
Whenever I try loading it through uiimageview using a uiimage like following, Here expanded_photo is imageview which I'm creating in a nib file:
UIImage *image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:photo_url]]];
expanded_photo.image = image1;
This takes a lot of time to load.
I want a solution which can load image from url in a small amount of time and the image is not cut.
Can anybody please help?
Thanx in advance.
Be aware that -[NSData dataWithContentsOfURL:] loads the data synchronously and thus blocks your main thread until the download is finished. When you load multiple images that way, all those loading operations are executed sequentially. This is not only slow but also your app is unresponsive during loading.
You might consider moving the actual load request in a background thread (using NSOperationQueue) or use NSURLConnection to asynchronously load the image.