简体   繁体   English

如何从磁盘读取图像并阻塞直到读取?

[英]How to read an image from disk and block until its read?

I'm loading 5 images from disk but I want the 1st image I read to show as soon as possible. 我正在从磁盘上加载5张图片,但我希望我读到的第一张图片尽快显示出来。 Right now I do 现在我做

UIImage *img = [UIImage imageWithContentsOfFile:path];

in a for loop but since imageWithContentsOfFile:path isn't blocking all 5 images wind up getting read from disk before the first one will appear (because they're all using up I/O). 在for循环中,但是因为imageWithContentsOfFile:path没有阻塞所有5个图像,所以在第一个图像出现之前最终会从磁盘读取(因为它们都在耗尽I / O)。 I confirmed this by just load one and it appeared on screen faster than if I load all 5. 我通过加载一个确认了这一点,它比我加载所有5更快地出现在屏幕上。

I'd like to load the 1st image and block until its fully read and shown before loading the next 4 or load the 1st one and be notified when its done before loading the next 4 but I can't figure out a way to do it. 我想加载第一个图像并阻塞,直到它完全读取并显示,然后加载下一个4或加载第一个图像并在加载下一个4之前收到通知,但我无法找到一种方法来做到这一点。 None of the 'WithContentsOfFile' methods for data or image block and they don't notify or have delegate methods. 没有用于数据或图像块的'WithContentsOfFile'方法,它们不通知或具有委托方法。 Any suggestions? 有什么建议?

Thanks 谢谢

You can ensure that the image is fully loaded from disk by first loading the image file into a NSData object and then initialize the image with the data: 通过首先将图像文件加载到NSData对象中,然后使用数据初始化图像,可以确保从磁盘完全加载图像:

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

Claus 克劳斯

AFAICS there is no way to guarantee that image data is in memory. AFAICS无法保证图像数据在内存中。 The documentation on initWithContentsOfFile says that 关于initWithContentsOfFile的文档说明了这一点

This method loads the image data into memory and marks it as purgeable. 此方法将图像数据加载到内存中并将其标记为可清除。 If the data is purged and needs to be reloaded, the image object loads that data again from the specified path 如果数据被清除并需要重新加载,则图像对象将再次从指定路径加载该数据

So OS might free the memory at any moment and make your application to load image again from file. 因此操作系统可能随时释放内存并使您的应用程序再次从文件加载图像。 However, I think you can try to do some tricks like calling method that requires actual data. 但是,我认为你可以尝试做一些技巧,比如调用需要实际数据的方法。 You can start by calling size of returned image. 您可以从调用返回图像的size开始。 I'm not sure that size really needs all image data and is not cached. 我不确定该size真的需要所有图像数据并且不会被缓存。 You might have to call something other methods on underlying CGImageRef . 您可能必须在底层CGImageRef上调用其他方法。 Something like CGImageGetBytesPerRow might do the trick. CGImageGetBytesPerRow这样的东西可能会成功。 But I'd first start with size 但我首先要从size开始

You want to load the image in the background and then have a call back when its done loading so you can display it...this way the block from reading the images wont effect you and you can display images while others are loading, or do whatever you like.. something like 您想要在后台加载图像,然后在完成加载后回调,这样您就可以显示它...这样,读取图像的块不会影响您,您可以在其他人加载时显示图像,或者无论你喜欢什么......类似的东西

-(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 load the images as youd like.. you can pass extra variables in there so you can know which image you are loading, or come up with something to keep track, but using something like this should be able to solve your problem... 你可以使用它来加载你想要的图像..你可以在那里传递额外的变量,这样你就可以知道你正在加载哪个图像,或者想出跟踪的东西,但是使用这样的东西应该能够解决你的问题。问题...

hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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