i'm writing a super simple photo sharing app for practice using parse.com as my server.
i have a tableview that just shows photos a user has uploaded to the server. if nothing is persisted and this guy has a million photos uploaded to the server, what would be the best way to fetch the photos?
I know that the simple way is to make one request on viewdidload and store everything in memory before persisting it. but is there a better way? ideally i could pull photos in batches, and as the user scrolled, it would pull new photos. perhaps there is a library you know of that helps with this?
If you don't want to do the entire thing yourself, you should use SDWebImage
A typical code example looks like
[imageView sd_setImageWithURL:[NSURL URLWithString:#"https://graph.facebook.com/olivier.poitrey/picture"]
placeholderImage:[UIImage imageNamed:#"avatar-placeholder.png"]
options:SDWebImageRefreshCached];
Related
I'm working on a news aggregation project like Flipboard. And lots of the images are the same in UITableView feed list and corresponding content rendered by WKWebView. It would save significant web traffic if a share cache exists.
Is it possible? Thanks in advance for your response.
Simple answer, at the moment this is not possible. You could think of a way to actually display the images in a WKWebView, but that might be, depending on your use case, quite impractical.
I'm wondering if you can help me solve more of a general, process question.
In my app when the user signs up, I create a thumbnail avatar (approx 35x35 px). I upload this to the server.
In another section of my app I display all users in a UITableView. This table is populated by a request that occurs on the view controller beforehand (so I can reduce loading screens).
This table contains approximately 100 rows. My question is, how do I load all the avatars seamlessly?
I dont want the user to tap the view and see a bunch of users, then avatars loading in randomly.
I dont want a loading screen to appear, unless its absolutely necessary (i.e. the user navigates through the app quickly).
I dont want to fire off 100 requests at once to my server.
I would like to send one request, and possibly get them back all at once. And if I did this, is there a specific format I should use? A big JSON response with base64 encoded thumbnails? :/
In addition to this, should I cache these? If so, how?
Swift 3.
Thanks!
SDWebImage is a really amazing framework that you can use to achieve this task. I've used it in every app of mine.
You can find it here: https://github.com/rs/SDWebImage
You can just put the following code in your cellForRowAtIndexPath Method
cell.imageView.sd_setImageWithURL(String.initWithURL("http://www.domain.com/path/to/image.jpg"))
The URL specified here is the link to the individual image to be shown in that particular row. This way your app can load images as the user scrolls the tableview. Given that your images are 35x35, it should be smooth and quick.
Hope this helped!
After more research based on Pratham Mehta's answer, I found a library called KingFisher:
https://github.com/onevcat/Kingfisher
This is more appropriate for my project, given SDWebImage is written in Objective-C, and my project is in Swift 3.
Check out Nuke. It's designed to support large collections of images (has built-in throttling, rate limiting of requests, etc).
And if I did this, is there a specific format I should use? A big JSON response with base64 encoded thumbnails? :/
I would suggest to avoid any premature optimizations - just download the images lazily one by one as soon as they appear on the display. Most image loading frameworks are designed to support this really well.
In addition you can prefetch the images.
If you want to do it the hard way - just zip the images and download the archive.
In addition to this, should I cache these? If so, how?
You may find this helpful.
I wanted to know how it is possible to make asynchronous request of images, and display them in a scroll view in a way that I can scroll the images without making requests for each images. I read some threads that gave me some ideas, so I think that I have to stock the urls in an array, and then I don't know what to do. If someone can explain to me ? or have a concrete idea on how to do this ?
NSScreencast has a video that does something similar. It is also a good video to learn about blocks too.
http://nsscreencast.com/episodes/10-fun-with-blocks
in order to load images asynchronously there are many options. The best approach is to create a different thread and fetch the url.
Sometimes its better to used tested and trusted apis available. I will suggest you to use SDWebImage
This will not only help you to load images asynchronously but also provides the caching mechanism. Which you will later or sooner user in you app development.
Its easy to use. Happy Coding :)
I'm looking to swap out the AFImageCache used by default in the UIImageView+AFNetworking category for something that's disk based and that can managed a little more accurately (something like NSURLCache). Unfortunately, since UIImageView+AFNetworking is a category and not a subclass, I can't just override af_sharedImageCache with a sublclass of UIImageView OR another category.
Is there any other way to achieve this functionality without copying and pasting most of UIImageView+AFNetworking into my own subclass?
The SDWebImage project provides a similar UIImageView category, but offers both in-memory (using NSCache) and on-disk (using NSFileManager) caching. I'd recommend just using that when you need to cache to disk.
The downside to this implementation is that your network requests won't go through your AFHTTPClient subclass, so depending on what your needs are you might need to implement your own operation queue, authentication, etc. If you're just using it for something basic, like displaying avatar images in a table view, it should be fine.
If that downside bothers you, an alternate idea would be to use SDImageCache (included in the SDWebImage project) to cache the images, and download them yourself using AFNetworking.
Finally, note that AFNetworking has built-in support for NSURLCache, and if you create one it will cache your images to disk. However, image caching is typically used for showing lots of images in a UIScrollView, and NSURLCache doesn't have good enough performance for smooth scrolling.
I have a fork of AFNetworking that includes file cache in the NSCachesDirectory.
You can find it here: https://github.com/andyast/AFNetworking_FileCache
There is a branch that is compatible with V1.3.3 if you need that as well.
I'm developing an iOS app and my idea is to display all Instagram images after a certain timestamp. I use HSInstagram to handle the API response and put all the images in a UIScrolView.
In order to do so, I do a request to the following path: users/%#/media/recent/?acces_token=MY_ACCESS_TOKEN&min_timestamp=1350000000. But the response I get is just a limited number of images, which aren't all the images after my timestamp.
I thought it was due to the limit that instagram puts on 20 images every request and thus I also tried to do the request to users/%#/media/recent/?acces_token=MY_ACCESS_TOKEN&min_timestamp=1350000000&count=-1 to avoid the limit. But in this case, the images didn't even load.
I'd appreciate any contribution. Thanks for your time!
As HSInstagram code was not prepared to implement the feature I wanted to implement, I foud an alternative called GrabKit in which you can also import your Facebook and Picasa images among others although you can disable them if you want to.
I hope this library can help you if you want to build an application that supports Instagram pictures the same way the app I'm building does. I really recommend it if you want to save thousands of lines of code as well as many hours dealing with APIs.