简体   繁体   中英

Choosing correct way to write Images to folders for iOS

I want to write Images to folders which are setup by me at run time. Now suppose there are 100 images, if I save it in two folders namely folder1 and folder2 both in Documents Directory. Both having equal no. of images ie 50 in folder1 and remaining 50 in folder2. Next I want to fetch them wherever I need to.

So my question is, Is it right approach to choose two folders for half number of images or Just one folder for all the images. Which will be faster to read and write? Also what happens if number of images increase in single folder or more number of folders with some fixed number of images in it(like 5 folder each with 100 image or 1 with 500 images)?

I will be saving those paths in one field of my sqlite database. Also I will be querying for this path and showing the images and for now I want to do this for iOS device only. Thanks.

Vish, if you already have the image file path stored in your sqlite file, it doesn't matter how many files you will store - I don't think that NSFileManager needs to go over all files. If you needed to go over the files, check the names, and only then open the images, that would be a different story and you would have performance issues. So do everything in one folder. I would suggest only two folders if you needed to save different kind of images, let's say, a cover image and a profile image for a user, and you would create one folder for covers and one folder for profile images.

You can use Grand Central Dispatch's dispatch_async for saving the images, so you won't block the main thread and UI. You could do it this way:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    //save here the images
});

If you want to read more about NSFileManager and performance tips, take a look here .

There's unlikely to be any performance benefits to writing to two folders instead of one. The NSFileManager code will handle all that for you anyway. Also you could make use of either dispatch_async or NSOperationQueue to keep the activity in the background and not block the UI which would give you better ways to manage performance.

If you're storing the paths in a persistent data store like sqlite then it makes sense to keep the paths to the files as simple as possible so again one folder would be best.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM