Swift: Loading Images Asynchronously and storing with NSCache and NSURLCache

Поделиться
HTML-код
  • Опубликовано: 4 янв 2025

Комментарии • 122

  • @ParthAnand
    @ParthAnand 8 лет назад +10

    you are the best! this is an understatement for you. please never stop making tutorials.

  • @locacocavola
    @locacocavola 8 лет назад

    I love the way you implemented the activityIndicatorView on each StatusImageView.
    Again, thanks for what you're doing.
    Happy Sunday.

    • @LetsBuildThatApp
      @LetsBuildThatApp  8 лет назад

      Thanks Andrea, going the extra mile to demonstrate a concept goes a long way.

  • @TheKaushal8686
    @TheKaushal8686 8 лет назад

    Your way of explaining things is way super than others.... thanks for yet another amazing video....

  • @samuelpmilanez
    @samuelpmilanez 4 года назад

    And before i forget, congratulations! Your videos are amazing !

  • @pasib
    @pasib 8 лет назад +1

    Once again an excellent tutorial! This one saved me a lot of time. Thank you Brian!

  • @shivamroy1775
    @shivamroy1775 8 лет назад

    You are awesome . I actually learn everything in so detail in your channel . You're great .

  • @yogitamunde6134
    @yogitamunde6134 7 лет назад +2

    Nice tutorial.all cache basic is covered ,,,Really helpful...

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 лет назад

      Indeed, however there is much more about caching that I wish to go over in later videos.

  • @TheYannsonnboys
    @TheYannsonnboys 8 лет назад

    Your tutorials are amazing man.
    Thanks for that

    • @LetsBuildThatApp
      @LetsBuildThatApp  8 лет назад +1

      +Yannsonnboys Yannick Great to be of service to you.

  • @SteveWoznokav
    @SteveWoznokav 7 лет назад +3

    Hi Brian can you please make another video on how to properly clear the cache when it is full using different methods?

  • @akzuki1210
    @akzuki1210 8 лет назад

    This is amazing. Thanks a lot.
    Waiting for more tutorials

  • @abhisheksitar
    @abhisheksitar 8 лет назад

    Amazing tutorials , learnt a lot of stuff and made me a better programmer. Thank you.

  • @csrkn
    @csrkn 7 лет назад

    Thank you so much man. You really helped me a lot. Please keep up the great work!

  • @infohpreet
    @infohpreet 2 года назад

    Great tutorial!! Thanks!!!

  • @michaelkornelakis8507
    @michaelkornelakis8507 8 лет назад

    Man you are amazing! Thanks once again!

  • @thanasistratigakis9360
    @thanasistratigakis9360 8 лет назад +1

    Great tutorials! Subscribed

    • @LetsBuildThatApp
      @LetsBuildThatApp  8 лет назад

      +Thanasi Stratigakis thanks a lot, lots more coming!

  • @guduruvamshikrishna7355
    @guduruvamshikrishna7355 7 лет назад +1

    Finished project with Latest Swift syntax here:
    github.com/VamshiIITBHU14/FBClone

  • @art4ho
    @art4ho 6 лет назад

    Thanks again for your sharing!!! Really hero

  • @AnuragKashyap17
    @AnuragKashyap17 7 лет назад

    Great Tutorial :D Thank you for this :) keep up the good work.

  • @megaman9422
    @megaman9422 5 лет назад +2

    there's something missing between this video and the one before it, in the one before it you didn't have FeedCell file and image url was not yet set up.

  • @VivekVerma1993
    @VivekVerma1993 8 лет назад +3

    Is it possible to make image height dynamic in the cell, as you have done with status textview.
    PS: when you have url of that image(dynamic content)

  • @laujimmy9282
    @laujimmy9282 6 лет назад

    Thx for the great tutorial

  • @haleemmilk
    @haleemmilk 8 лет назад

    You are amazing thanks alot for sharing your knowledge

  • @shivakumarhiremath8013
    @shivakumarhiremath8013 6 лет назад

    Hi .. I am big fan your turorials..in this video you said how to use Nsurlsache ,can you tell us how to clear or reset the Nsurlcache when Nsurlcache becomes full

  • @samuelpmilanez
    @samuelpmilanez 4 года назад

    What the better way? Use the default cache provided by NSURLSession or use my own cache maked using NSCache class?
    If i use NSURLSession and implement a cache using NSCache, therefore some images are cached twice? (NSURLSession and my NSCache object)

  • @david-tracy
    @david-tracy 6 лет назад

    Great work

  • @rameshannadurai4523
    @rameshannadurai4523 6 лет назад

    Excellent tutorials. How did you add ActivityIndicatorView to FeedCell while downloading the image. It is not explained in this video, Is it in different video?

  • @billionbucks5391
    @billionbucks5391 8 лет назад

    great job , best of luck man :)

  • @EvaristusEzekwem
    @EvaristusEzekwem 7 лет назад +1

    Awesome tutorials I must say Brian. You have influenced a lot of my coding style thanks a loooooooooooot. There's actually a gap between this video and the previous one. Some files (e.g Posts.swift, Extensions.swift, FeedCell.swift) weren't in the previous video. I have tried to recreate these files myself but I keep running into errors from the FeedCell.swift file. Please help. Thanks.

  • @johnhan800
    @johnhan800 8 лет назад

    Excellent tutorial! Thank you so much, for saving my homework!! lol

  • @aragorn392
    @aragorn392 7 лет назад

    Thanks! Is there any reason you used a dataTask vs a downloadTask? Wouldn't you want to use a downloadTask for images?

  • @abhinaykumar1045
    @abhinaykumar1045 6 лет назад

    Hi Brian, Hope you are doing good. Such a nice tutorial.
    I have a question when should I use this tutorial method and when should I use third party lib like AFNetworking etc.
    Thanks in advance:)

  • @david-tracy
    @david-tracy 6 лет назад

    Would you need to change anything in case you were getting dynamic data?
    (some data may not be there anymore, new posts added)

  • @GameCrawl
    @GameCrawl 8 лет назад

    The library that you mentioned at the end is this github.com/rs/SDWebImage?

  • @dillionwang5863
    @dillionwang5863 8 лет назад

    Good lesson! Veay helpful

  • @pankajchauhan9185
    @pankajchauhan9185 5 лет назад

    Hey, Brain I have a tableViewCell in which I am getting dynamic image size according to which I have to calculate image height but while using this code for getting image height.
    let imageURL = URL(string: strImageUrlPath)
    var imageData: Data?
    if imageData == nil {
    if let imageURL = imageURL {
    imageData = Data(contentsOf: imageURL)
    }
    }
    var img: UIImage? = nil
    if let imageData = imageData {
    img = UIImage(data: imageData)
    }
    scrolling of table view got stuck or some times zerk while scrolling .
    please give me some solution for that.

  • @Zakawaka45
    @Zakawaka45 8 лет назад

    Great video! But i have to ask, how did you learn about caching in swift, theres barely anything about it online

  • @thinkoreans
    @thinkoreans 8 лет назад

    Thanks for another great tutorial. I'm quite new to Swift but I see your way of set things up programmatically is somehow more organized and easier to see the whole picture. But I'm still on two minds whether I code my project without storyboard because I'm afraid I can't find good lessons on the internet when I hit a bump in the road. As you know, almost all the lessons use storyboard and your way is quite special. Can you suggest me any advice on this? What can be the best way to improve my coding or solve any issue when I do it programmatically?

    • @LetsBuildThatApp
      @LetsBuildThatApp  8 лет назад +1

      Like you mentioned above, doing things programmatically is more organized and easier to follow what is going on. Here's a statement that usually holds true: If you know how to do something in code, you will most likely know or will be able to figure out how to do it in a storyboard. I can't say that the inverse holds true.
      Also the major difference for me is that while I can use storyboards for a lot of projects, I can use the programmatic approach for all projects.

  • @nicholaslue
    @nicholaslue 8 лет назад +2

    I'm tying to figure out how to do this with videos. I'm guessing it's different because the action to play the video calls the URL everytime.

    • @LetsBuildThatApp
      @LetsBuildThatApp  8 лет назад

      Hello Nicholas. You should be able to perform similar caching if you can get access to the downloaded video file. I've never had to handle this but I'd be surprised if this issue hasn't been solved on StackOverflow.

  • @raymondong4504
    @raymondong4504 6 лет назад

    Thanks for putting this together. Really helpful stuff for getting me started with ios dev. Really interested with the paid lessons on your site. Just a question though. There seems to be missing video in this series; factoring out the data and code for the loader and server calls. Tried searching from your list but couldn't find it. Thanks so much.

    • @LetsBuildThatApp
      @LetsBuildThatApp  6 лет назад

      Not sure if I actually show how to refactor the code. It's just simple practice to clean up projects after a while.

  • @aboutcarsyo
    @aboutcarsyo 7 лет назад

    Do you think the best way to link the tableView data to the imageViews etc. is with the didSet inside the custom cell class like you did here? I'm just asking because I used to link it in cellForRowAtIndexPath and since this video is a little older by now, maybe you found another way you would consider as better? If not I will change my code and also use didSet inside the cell class :) Thanks for you great videos, I learned a lot from you!

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 лет назад

      It is best to keep your controllers really simple and not have them be aware of how cells are rendered. This way you implement separation of concern for your classes.

    • @aboutcarsyo
      @aboutcarsyo 7 лет назад

      Okay thanks a lot for your answer :) also performance wise it should be pretty good? Cause I had some problems with the scrolling sometimes it was a little choppy, just very little but noticeable

  • @joaquincorrales7383
    @joaquincorrales7383 8 лет назад

    Brian, thank you so much for the great tutorials. I have a question- would you have any idea why the scrolling in the collectionview seems very choppy even caching the images? Thanks

    • @LetsBuildThatApp
      @LetsBuildThatApp  8 лет назад

      Hi Joaquin, it could be your simulator that is choppy. Also, make sure you are storing the UIImage in the cache, also make sure the cache is actually getting back your uiimage objects instead of making another url request.

    • @joaquincorrales7383
      @joaquincorrales7383 8 лет назад

      Lets Build That App Hey Brian thanks for the reply. I'm actually experiencing it on my phone as that's where I'm testing on. The images are being cached and not being fetched more than necessary. I have also tried upping the default cache. Any other pointers? Thanks a lot man. Huge fan

  • @CaffieneToCode
    @CaffieneToCode 7 лет назад

    You are a champ Brian!
    Your continuous efforts in making people understand things practically are much appreciated.
    I tried the way of caching that you explained in RUclips app.
    Somehow it doesn't work. Is there a change in implementation for swift 3/4?

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 лет назад

      I would recommend looking at my cache implementation from LBTAComponents or browsing the source code for SDWebImage.

  • @AlessandroArcidiaco
    @AlessandroArcidiaco 8 лет назад

    Hi Brian, thank you for this amazing tutorial. One question: the images remains in cache when the app is in background mode?

  • @MohitJain14
    @MohitJain14 8 лет назад

    Great! Thank you.

  • @firstinline101
    @firstinline101 7 лет назад

    Really enjoyed this tutorial, thought it was great/interesting. Do large companies like Instagram or Facebook that have a lot of content in the form of photos/videos use similar NSURLCache methods?

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 лет назад

      Photos are definitely cached using their own sophisticated library where items are evicted in a very efficient process. You gotta go to one of their talks to see what is really under the hood for their apps.

    • @firstinline101
      @firstinline101 7 лет назад

      Ok, just wondering how translatable code is between smaller scale developers and the big boys! Thank you for your reply.

    • @david-tracy
      @david-tracy 6 лет назад

      How can we find these talks?

  • @dopors
    @dopors 4 года назад

    Will this method also being use by WKWebView? let say i have a video intro(intro1.mp4) and of course within the WKWebView you are sending request(using interceptor(URLProtocol) you can browse each request within). If not are there any caching method that you can use for faster loading video?

  • @Somerandomname23
    @Somerandomname23 8 лет назад

    Hi, it's also work, when I disappear this view, and back to him? whether will be download again? I mean in NSURLCache way.

  • @dzaev3663
    @dzaev3663 8 лет назад

    You lecture is really awesome. I learn the depth of swift from you. Thank You so much.
    And I have a quesiton.
    What do you refer at the starting point? I mean, I proceed to study in the order of this "How to create Facebook's news feed" playlist, but maybe it's different between a completed source code of "Dynamic cell content" part and the code of this lecture at starting point. So what the other viewers refer? I wonder how to proceed, thank you.

    • @LetsBuildThatApp
      @LetsBuildThatApp  8 лет назад +1

      Hello David, You should watch the entire FB tutorial series using this playlist: ruclips.net/p/PL0dzCUj1L5JHDWIO3x4wePhD8G4d1Fa6N

    • @dzaev3663
      @dzaev3663 8 лет назад

      Thanks for your reply, and I did refer the same playlist as you send me. But the source code is different, neither is the feed images. In the last part of "Dynamic cell content" lecture, the feed images is like DOG picture, JOBS picture seeing at 24:53. But in this lecture at start point, the feed images is totally different. Same in the source code.

    • @LetsBuildThatApp
      @LetsBuildThatApp  8 лет назад +1

      Yes the code changes a bit. Apologies, I didn't intend for these episodes to be continuous. These videos were shot way way way back in feb/march, I was still getting used to presenting code while recording. From now on, I'll create continuous content that will be easy to follow.

    • @dzaev3663
      @dzaev3663 8 лет назад

      I see. Thank you!!

  • @PanagiotisGoutzianas
    @PanagiotisGoutzianas 7 лет назад

    Hi can i ask something?What happens if there is no internet connection , can someone see the images that were cached before?

  • @VictorAlves-ur7lp
    @VictorAlves-ur7lp 8 лет назад

    very nice explanations , but you do not use the storyboard?

    • @LetsBuildThatApp
      @LetsBuildThatApp  8 лет назад +2

      +Victor Alves I like typing everything in code, call me old school :)

    • @BrettCWX
      @BrettCWX 8 лет назад

      Oldschool

  • @laiqueahmed6613
    @laiqueahmed6613 7 лет назад

    Hi Brian thanx a lot for the video. But I have few questions 1. What is the size of default cache ? I saw in the above video 500KB image was fetching from network but 32KB image was in cache I guess it loaded faster. 2. Does AlamofireImage library takes care of caching ? 3. What is the maximum size I can define for cache ? you have set diskcapacity 500 * 1024 * 1024 ? How many MB's ?

  • @kylim7523
    @kylim7523 6 лет назад

    setSharedUrlCache in Swift 4.2?

  • @OrkhanAlizade
    @OrkhanAlizade 7 лет назад

    Thanks for he tutorial! But why there is a gap between this and previous video? In which part have you wrote that new code part???

  • @joshivanilla
    @joshivanilla 8 лет назад

    First of all thanks for the tutorial!
    I have a question: Currently I'm using Firebase to store and fetch image.
    But I am experiencing a problem where the image are in the wrong tableviewcell even though the image urls in each cell are correct.
    Google told me that it was because the image files are too large and images are being overwritten (something to deal with async as well)
    Are there any ways I can solve this?

    • @LetsBuildThatApp
      @LetsBuildThatApp  8 лет назад +3

      I run into this quite often, and ran into this while using firebase as well. I'll address it with my solution in a Firebase tutorial soon.
      Thanks for watching.

    • @joshivanilla
      @joshivanilla 8 лет назад

      +Lets Build That App Awesome! Subbed and thanks!

  • @abdallahsultan8797
    @abdallahsultan8797 8 лет назад

    hiii,,
    what if i want to change the image without change the link..is that possible?
    if not>> can i load set of images (i will replace it weekly) from firebase to my app ?
    if not>> any solution plz??

  • @kongli886
    @kongli886 8 лет назад

    Every time the application exits the cache is also cleared. How can I make the application not clear the cache once it exits? I don't want my app to load the images user once loaded every time the app is reopened. It's just like your web browser, your files are stored in a temporary folder until you delete the temporary files

    • @LetsBuildThatApp
      @LetsBuildThatApp  8 лет назад

      The cache will not help you if you're trying to keep a persistent state of your files. I suggest looking into CoreData to store your files using a binary format. Another method is to store the files directly into your app.

  • @eyalby111
    @eyalby111 8 лет назад

    Hi, I don't think there is a good video tutorial on using AlamofireImage. if you can make one (using all his features and explaining them) it will be great!

  • @dany3370
    @dany3370 6 лет назад

    11:38 swift 4 update:
    var imageCache = NSCache()

  • @nverabgaryan
    @nverabgaryan 4 года назад

    what about video preloading ?

    • @LetsBuildThatApp
      @LetsBuildThatApp  4 года назад

      I suggest using HLS for video playback speed improvements.

  • @daurenmuratov7
    @daurenmuratov7 7 лет назад

    Awesome video! But, right now I'm having a problem. Every time collectionView bounces of the top, the first cell lags while going back to its position. Did Anyone have such problem?

  • @caelandailey6438
    @caelandailey6438 7 лет назад

    I have an issue. It'll set the cell's image to a previous cells image, and then switch it a fourth of a second later to the correct image. What am i doing wrong?

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 лет назад

      +Caelan Dailey probably cell recycle affecting your app

    • @krovogadniy
      @krovogadniy 7 лет назад

      same problem. is there a solution?

  • @roark09
    @roark09 8 лет назад

    Brian I'd like to ask if I can follow you on facebook or twitter.

  • @janardhanmupparaju2940
    @janardhanmupparaju2940 6 лет назад

    Hi,
    I am trying to load 100 or more images in tableview using SDWebImage but after downloaded all images getting Error Message from debugger: Terminated due to memory issue and after App is Crashing

    • @LetsBuildThatApp
      @LetsBuildThatApp  6 лет назад

      Cool, check the memory usage of your app

    • @janardhanmupparaju2940
      @janardhanmupparaju2940 6 лет назад

      thanks for your reply, now i identified problem is loading high resolution images in tableview

    • @LetsBuildThatApp
      @LetsBuildThatApp  6 лет назад

      Yeah you'll bust the memory limit real quick with high res images.

  • @pavanteja1996
    @pavanteja1996 8 лет назад

    sir is it possible to link Facebook's news feed to this app

    • @LetsBuildThatApp
      @LetsBuildThatApp  8 лет назад

      Hello Pavan. Unfortunately, Facebook does not allow developers to replicate behavior similar to their iOS app.

    • @locacocavola
      @locacocavola 8 лет назад

      What about twitter?

  • @tapizquent
    @tapizquent 6 лет назад

    Why not compress the image instead of bumping the cache up?

    • @LetsBuildThatApp
      @LetsBuildThatApp  6 лет назад

      Why not both?

    • @david-tracy
      @david-tracy 6 лет назад

      would we then need to decompress the image after grabbing it from the cache and presenting it to the collection view?

    • @tapizquent
      @tapizquent 6 лет назад

      CDMusicGroup no, why would you? Since you’re displaying the imagine on the screen, you don’t need it to be a huge image. Just like Instagram, images are very low res cause you don’t need a huge image for a phone

    • @david-tracy
      @david-tracy 6 лет назад

      Tapizquent ok, sounded like you were saying to compress it into a file of a different (similar to how a .zip is compressed). I’m now seeing you’re referring to resizing and reducing the resolution of the image. Am I understanding you better now or are we still not on the same page

    • @tapizquent
      @tapizquent 6 лет назад

      CDMusicGroup you are right. I was just referring to saving a smaller image.

  • @leanprogrammer
    @leanprogrammer 7 лет назад

    (Min 6)Is it possible that there's no need to cache anymore(as of October 2017)? Because my images do not reload. It is as if the network request is made once and the image is automatically cached. Anyone experiencing the same thing? Thanks in advance

    • @LetsBuildThatApp
      @LetsBuildThatApp  7 лет назад

      It all depends on how fast your internet connection is. Caching comes into play on 3g and much bigger apps.

    • @leanprogrammer
      @leanprogrammer 7 лет назад

      Oh okay, thanks Brian

  • @learning2886
    @learning2886 8 лет назад

    Make a Udemy tutorial. Id buy it instantly. intermediate to advanced topics for the swift language is a soft market there and you're course would sell like fire. just sayin'... I spend 50$ a month on courses there.

    • @LetsBuildThatApp
      @LetsBuildThatApp  8 лет назад +1

      +Hunter Walker Thank you for the info regarding udemy. With the pricing changes and strictness of that platform, I'd have to continue monitoring before I step into that world.

  • @pvane0123
    @pvane0123 5 лет назад

    wasted 20 min... this is no longer applicable