I have an app that presents a bunch of thumbnail images and they are automatically cached using:
WebImage(url: URL(string: currentWallpaper.thumbnailURL), options: SDWebImageOptions.retryFailed)
This works great. When a user selects a thumbnail I present a full screen version:
WebImage(url: URL(string: currentWallpaper.hiresUrl), options: SDWebImageOptions.retryFailed)
When a user displays an image full res I have the ability to swipe to present the next or previous hires image without going back to my thumbnail catalog.. so I prefetch both the previous and next image so it feels immediate when they swipe.
After a while loading images gets sluggish. I'm thinking I'd like to setup a separate cache for the hires images so when the user goes back to the thumbnail view I can clear it but still keep the thumbs in cache. Or just limit the number of images in that cache, something like.
I'm just not finding any examples of how to point WebImage to a custom cache. I see how to create one:
let hiresCache = SDImageCache(namespace: "hiresCache")
SDImageCachesManager.shared.addCache(hiresCache)
Just not exactly how to use it. Ideally WebImage just allows you to pass a cache to it, but I'm not seeing that as an option. Or maybe someone has a better solution
Related
Which is the most preferred method for an in-app web browser? My app has the need to have a toolbar at the bottom, and I need to be able to take screenshots of visited web pages by hitting a button on the toolbar.
Here is what I'm running into. I want to be able to click a link in my app, open an in-app browser, take a screenshot, and save to core data along with other information about the site.
I'm able to save a screenshot to the camera roll with .takesnapshot() method for Webkit. I've been unable to save it to core data. As of last night, I've found a few functions on SO that show how to take a screenshot of the UIView and return a UIImage, but I've been unable to cast this back to Data since this is what Core Data expects for binary data. Does anyone have a good resource to save Webkit snapshots to Core Data?
In one of the functions I attempted last night, I was able to return the UIImage object, but I was unable to convert it back to Data. I can save all other data about the site to Core Data, but I'm unable to save the image - in fact, when I attempted to save the data directly with Webkit's .takesnapshot() method, the result was nil.
I'm going to answer your question in two parts.
WebKit vs SFSafariViewController
If you want to have a custom toolbar with a button for taking screenshots, I would use a UIViewController with a custom tab bar where you can add whatever buttons you want, then I would also embed a WebKit view in the view controller too. That should be pretty straightforward.
Saving screenshot to Core Data
By the sounds of it, you may be getting a screenshot the wrong way, you want to do so with graphics begin and end image context as follows:
let layer = UIApplication.shared.keyWindow!.layer
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale)
layer.render(in: UIGraphicsGetCurrentContext()!)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
Now you have a screenshot saved as a UIImage? in your screenshot variable. Next up you should convert this to data with:
let data = UIImagePNGRepresentation(screenshot!)
Which should give you a data representation of your image that you can then save to Core Data.
I've been using HJCache to do asynch image loading and caching. It works great, but I have use case where I can't seem to get HJCache to handle.
I have an images loaded in a table view, and the same images appears bigger when you select the corresponding tableviewcell. HJCache does a great job of caching the image so it doesn't have to reload it after getting it once. However, I would like to resize the image (and do some cropping, etc) in the tableview thumbnail. The problem is, that it's a very expensive task to do and it ends up lagging the scrolling of the tableview if it's done in the drawRect of the cell.
I would like to cache the "modified" image along with the original so that the image processing only has to happen once. How do i get an instance of the already cached UIImage, apply the processing, and then add that one to the cache as well (with a different oid). I only seem to be able to cache an image by giving a URL.
Thank you,
Matt
I am trying to lazily load a dynamic thumbnail view.
I have to put images in thumbnail view one by one and until images are not available I have to display temporary image like placeholder. Any guidelines please?
See the accepted answer in this thread. You wouldn't need the last-in first-out stack, but it shows how to load images asynchronously using grand central dispatch, which is quite straightforward. You don't need to think about complicated thread management. :)
You could go about like this -
Create a UIImageView with a default placeholder image.
Fetch your image in a background thread. You want to do it like this
because, fetching image in the main UI thread blocks it till the
image is fetched, which severely hampers UI responsiveness. There
are several libraries available which do this for you. They even
cache the images. SDWebImage is great!
See if you get a valid HTTP 200 response back i.e. a valid image. If yes, then replace the new image with placeholder image. Else let the placeholder image be.
Hope this helps...
I'm making a simple rss reader for the iPhone. The feed (http://feeds.feedburner.com/foksuk/gLTI) contains a reference to images that I want to show in my TableView.
By default, as far as I understand, the iPhone only tries to load images when they are needed: it will only load the images for the rows that are displayed. Only when the user scrolls down, it will start loading the ones that need to be displayed. Right?
Now, since the images are downloaded from the web (they are not too large, but still), the scrolling of my TableView is stuttering. This is in the emulator, so on the device itself it will only be worse.
Luckily, this specific feed only lists the latest 12 entries of the blog. So I guess I should be able to first download an cache all the images, so they can be loaded from the memory, rather than from the URL.
What is the proper approach here?
I had the same problem, where my TableView would have to download each image before it displayed the cell. Like you say, it causes a big slowdown in the scrolling speed. What you need to do is download the images in the background and then insert them into the cell when they're finished downloading. This is how the app store app does it.
This post has all you need to know, including a class file you can use straight away:
http://www.markj.net/iphone-asynchronous-table-image/
I have some 50 custom cells in my UITableView. I want to display an image and a label in the cells where I get the images from URLs.
I want to do a lazy load of images so the UI does not freeze up while the images are being loaded. I tried getting the images in separate threads but I have to load each image every time a cell becomes visible again (Otherwise reuse of cells shows old images)
Apps like Facebook load images only for cells currently visible and once the images are loaded, they are not loaded again. Can someone please tell me how to duplicate this behavior.
Thanks.
Edit
Trying to cache images in an NSMutableDictionary object creates problems when the user scrolls fast. I am getting images only when scrolling completely stops and clearing out the cache on memory warning. But the app invariably gets a memory warning (due to size of images being cached) and clears the cache before reloading. If scrolling is very fast, it crashes.
Any other suggestions are welcome
Loading the images on a background thread is still a good idea. If you didn't want to reload them each time, I'd suggest setting up an NSMutableDictionary and storing the images in there. You could use some unique identifier, like the row ID or even the name of the image, as the key for each image.
When loading a cell, you'd send an objectForKey: message to the NSMutableDictionary to retrieve the image for that particular cell (based on your unique key for it). If it returns nil, that means that the image is missing from the cache and you need your background image loading thread to go retrieve it. Otherwise, you will get back the appropriate image for your table cell to display. On a memory warning, you could clear out this cache of images with no adverse effects (aside from forcing them to be reloaded again on demand).
I have just successfully tackled the same problem by using a custom NSOperation to load the images in a queing fasion and stored them into a static NSMutableDictionary as a cache. Below is a link to the basis of the code I used to solve the problem.
Loading remote images for UITableViewCell
Best to read all the threads in the forum to help you understand what's actually going on.
lostInTransit,
I am having a similar problem and while exploring the many different possible solutions I found this blog post:
davidgolightly.blogspot.com/2009/02/asynchronous-image-caching-with-iphone.html
I would also suggest that you download the URLCache sample from the apple developer website:
developer.apple.com/iphone/prerelease/library/samplecode/URLCache/
And here is another post on the problem:
www.markj.net/iphone-asynchronous-table-image/
I'd love you to share your findings as well.
Lazy loading is like synchronous type request.. means wait for respond
ego image button is solution for that..
ego image button is asynchronous type request..don't wait for respond..just display data at a time....
you can download folder from github....
add to your project...
in xib..at image view ,change class to ego image button...
make object of it in m file...
you can use.....
For those who are interested, and are lazy like me, I would like to suggest an open source (MIT license) implementation of lazy/cached network of UIImageView images: SDWebImage
UITableView with image caching and resizing/setting in background thread:
http://blog.slaunchaman.com/2011/08/12/gcd-example-updated-now-with-more-speed/
This is Tutorial about NSOperation with example that show how to Lazy load images in UITableViewCell