1

What I do in my app is when a user uploads profile pic, I write the imagedata in document folder, in other words I store the image. When a user removes profile picture, that file is deleted. So that is basic.

But I also remove this file when user logs out, because if I don't then if he logs out and logs in with different username, same profile pic appears. So What I do is, I at the time of login, download user's profile pic(pic URL is given in login API as I send this pic while uploading) in a global thread. But this is not the best solution as everytime user logs in, it takes time to download the image.

What is the best solution for this?

nhgrif
  • 61,578
  • 25
  • 134
  • 173
Hiren Prajapati
  • 717
  • 3
  • 10
  • 25

2 Answers2

1

You should store URL of the image and compare URL at the time of login. If both URL are same then don't download picture and display stored one. if both URL are not same then download the picture and stored it and it's URL. You can save any other unique flag instead of URL which is unique for each user (exa: user id).

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
1

if there is no specific requirement of storing the image in document directory you may try SDWebImage or AFNetworking UIImage library which provides image cache so if an image is downloaded for one time it will not going to download the same picture every time , else you can use NSCache directly if you comfortable with it

Another solution can be if you have to store user profile images you can store them in the individual folder/URL may be based on userId or some unique details so first you check if the specific image exists else download the image for ex: if userId is 123 then u store the image as 'imagename-123' or create a folder name 123 and store the image inside it
Note: If your image is too large or if too many users logged, in that case, your document directory may take much space so you may put some mechanism to delete older profile for the same

HardikDG
  • 5,892
  • 2
  • 26
  • 55