Downloading and using a large number of avatars - ios

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.

Related

Best practice for storing and updating images in Xcode project

I am building my flash card iOS app for reviewing my learning Japanese using SwiftUI language.
The problem is how to storing and updating my images(>500 images). Please help me, any suggestion is appreciated, thanks for reading my post.
I think you're asking about how to manage 500+ images in an Xcode project. You could just add all the images to your project and load them as you would any image. You could use asset catalogs, which have the advantage that they let you store different versions of a resource for use on different devices, and only the ones needed for the device the app runs on will actually be installed on the device. See How Many Images Can/Should you Store in xcassets in Xcode? and Asset Catalog Format Reference for more information about asset catalogs. But any way you slice it, managing 500+ images is going to be cumbersome. There's probably a better way...
Managing all those images in your app isn't just a problem for you as the developer; building them into the app will also create problems for the user. Even if each image is relatively small, having hundreds of them in the app will probably make the app huge. That means it'll take a long time to install, and the app will use a lot of storage on the device. Every time you release a new version of the app, with more words, or even just to fix a few small bugs, the user will have to download all that data all over again.
Instead, you should consider building an app that can fetch the data it needs from a server. Ideally, you could apply that approach to all your app's data, not just the images. Maybe you'll organize your flash cards into sets of a few dozen, so that you can fetch a set of cards and the associated images pretty quickly, and sets that the user hasn't used for a while can be removed to free up space on the device. You'll be able to update a set of flash cards without having to update the app, and when you do update the app your users won't have to download all the data all over again.
You've said that you're a beginner, so this approach might seems very difficult. That's OK, you can start with a simpler approach and then improve as you go along. For example, you might just put all the images on a server and fetch them one at a time as you need them. Your flash card data file could contain just a dictionary with words and the URLs associated with those words. There are lots of examples of loading an image from an URL here on SO and elsewhere, so I'm not going to provide code for that, but it won't be hard to find. The earlier you start thinking about how to design your app so that it can scale as you add more and more words, the easier it will be to maintain the app later.
500 images can have a huge size. Applications that published on Appstore have size limit and Apple does not recommend to make big apps.
Store them on server and load needed images on fly. Also you will get possibility to update your images, remove add new.
If you don't have a backend, you can use something easy and free (Firebase storage for example) or with minimal code writing on AWS.
If you need to keep them on device - store them as files in the Documents or another apps folder, do not use CoreData for it (you can keep only the list of names/urls in database).
After loading image to be displayed for user, you can prefetch next bunch of images.
Use Alamofire, or SDWebImage to load images from network (I prefer last). These frameworks can do many useful things with images.
To load images:
you can have a list of your images (just list of the names and urls)
or
you can know only path and names pattern and generate links dynamically (like https://myserver/imageXXX.png.

asynchronous request of images by url for scrollview ios

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 :)

Downloading and displaying big images: possible approaches

I'm developing a game where the main element of gameplay is an image of 2400x1600 pixels. So we are having some discussion about what's the best approach we could follow and confirm that we are not missing anything in order to download the image from the server and get it displayed on the device. So we thought about the different approaches and see what everyone else's thoughts are. I would really appreciate if you can list another approach you can think of. Before getting into the approaches themselves, some things that we need to take for granted:
User experience since gameplay started should be smooth. This made us get rid of one possible approach that was to implement it like Google Maps i.e. downloading tiles from the server as the user pans/zooms the map since there will be for sure some delay until the tile is finally displayed.
Images cannot be shipped within the binary file since they will be updated frequently.
All the tiles of each image weight around 6MB
So possible solutions we see so far:
Download and tiling the images in a background process/task all at once as they get updated. The big loading time here will be the first time the user launches the app. Images don't get updated together neither very frequently - one day minimum.
From the moment the user gets to the screen to select the image to play, start the process of downloading and tiling. User can select between 2 pictures only.
If user is on WiFi, use solution 1. If user is over cellular network then use solution 2.
Thanks in advance.
Use SDWebImage. It provides a ton of features for caching images, downloading in background, and much more.
The NSURLSession class in iOS 7 is well-suited for your needs. It allows for background downloads, resuming and pausing downloads, and is heavily customisable.
Now, I know this is a little vague, so here are some great references to start with :
Apple's NSURLSession Documentation
Mobile.TutsPlus NSURLSession Tutorials

Instagram MIN_TIMESTAMP not returning all images it should

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.

How to simply store some files

I'm developing an app which needs to show some logos. These logos are just 8kb PNG files, and I'm just going to handle a little amount of them (10-20 at most). However, these are downloaded from the Internet because they might change. So, what I'm trying to achieve is, making the app to download them (done), storing them into the file system, and only downloading again whenever they change (might be months).
Everyone seems to use Core Data, which in my opinion is something designed for bigger and more complex things, because my files will always have the same name plus don't have relations between them.
Is the file system the way to go? Any good tutorial?
Yes, the file system is probably your best option for this. You say that you've already implemented the downloading. How have you done so? With NSURLConnection? If so, then at some point, you have an NSData object. This has a couple of write... methods you can use to save the data to a file on the filesystem. Be sure to save the files in the right place, as your app is sandboxed and you can't write anywhere you like.
The advantage Core Data brings is efficiency. Using NSFetchedResultsController to display your logos in a tableview gets you optimized object loading and memory management. It will automatically load only the items which can be displayed on one screen, and as the user flicks through the table it will handle releasing items which move offscreen. Implementing that on your own is not a simple task.
If you want to build and display your data without Core Data, you'll probably want to use NSKeyValueCoder, which will allow you to easily write an array or dictionary of objects (including nested arrays, dictionaries, and images).

Resources