How to read an image from a disk and block until it is read?

I am loading 5 images from disk, but I want the first image that I read to be shown as soon as possible. I'm doing right now

UIImage *img = [UIImage imageWithContentsOfFile:path];

in a for loop, but since the imageWithContentsOfFile: path does not block all 5 images, they start reading from disk before the first one appears (because they all use I / O). I confirmed this by simply downloading one, and it appeared on the screen faster than if I downloaded all 5.

I want to download the 1st image and block it until it reads and reads completely before downloading the next 4, or download the 1st number and get a notification when it is done before the next 4 is downloaded, but I cannot figure out a way to do this. None of the "WithContentsOfFile" methods for a data block or image notifies or does not have delegation methods. Any suggestions?

thank

+3
source share
3 answers

You can verify that the image is fully loaded from disk by first loading the image file into an NSData object, and then initializing the image with data:

NSError *err = nil;
NSData *data [NSData dataWithContentsOfFile:path 
                                    options:NSDataReadingUncached
                                      error:&err];
UIImage *img = [UIImage imageWithData:data];

Klaus

+4
source

AFAICS , . initWithContentsOfFile ,

. , ,

, . , , , , . size . , size . , - CGImageRef. - CGImageGetBytesPerRow . size

0

, , ... , , , , , . -

-(void) getMyImage:(NSString*)path
then 
[self performSelectorInBackground:@selector(getMyImage) withObject:path];

you can also have a methods
-(void)didFinishLoadingImage:(UIImage*)image// which gets called in getMyImage when  the image is done loading...something like
[self performMethodInMainThread:@selector(didFinishloadingImage:)] etc...

You can use this to upload images as youd. You can pass in additional variables so that you can find out which image you are loading, or come up with something to keep track of, but using something like this should be able to solve your problem ...

hope this helps

-1
source

Source: https://habr.com/ru/post/1741106/


All Articles