简体   繁体   中英

How to efficiently read multiple images from documents directory in iOS

I am working on an app that saves multiple images in Documents directory. These images can be up to 100. Now use following method to read the image from Documents directory. This method is called for all images in Documents directory.

UIImage *currentImage = [UIImage imageWithContentsOfFile:pathOfFileInDocumentsDictory];

So in worse case this method will run for 100 images and I have checked using XCode that this method takes around 100 miliseconds. So this makes 10 seconds for 100 images if I am not wrong. I want to make it efficient. Is there any better way to read those image for efficiently and in less time?

Using run loops, you could do this:

-(void) loadInBackground {

    [self performSelectorInBackground:@selector(_loadInBackground) withObject:nil];

}

-(void) _loadInBackground {

    // Do all your heavy loading here
    UIImage *currentImage = [UIImage imageWithContentsOfFile:pathOfFileInDocumentsDictory];
    [self performSelectorOnMainThread:@selector(loadedImage:) withObject:currentImage waitUntilDone:YES];

}

-(void) loadedImage:(UIImage*)img {

    // Do something with the loaded image
    anImageView.image = img;

}

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