Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何在iOS中高效地从文档目录中读取多个图像_Objective C_Ios_Performance - Fatal编程技术网

Objective c 如何在iOS中高效地从文档目录中读取多个图像

Objective c 如何在iOS中高效地从文档目录中读取多个图像,objective-c,ios,performance,Objective C,Ios,Performance,我正在开发一个应用程序,可以在文档目录中保存多个图像。这些图像最多可以是100个。现在使用以下方法从Documents目录中读取图像。对文档目录中的所有图像调用此方法 UIImage *currentImage = [UIImage imageWithContentsOfFile:pathOfFileInDocumentsDictory]; 所以在更糟糕的情况下,这个方法将运行100幅图像,我用XCode检查过,这个方法大约需要100毫秒。如果我没有错的话,100张图片只需要10秒。我想让它更

我正在开发一个应用程序,可以在文档目录中保存多个图像。这些图像最多可以是100个。现在使用以下方法从Documents目录中读取图像。对文档目录中的所有图像调用此方法

UIImage *currentImage = [UIImage imageWithContentsOfFile:pathOfFileInDocumentsDictory];

所以在更糟糕的情况下,这个方法将运行100幅图像,我用XCode检查过,这个方法大约需要100毫秒。如果我没有错的话,100张图片只需要10秒。我想让它更有效率。有没有更好的方法可以在更短的时间内高效地读取这些图像?

使用运行循环,您可以执行以下操作:

-(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;

}

感谢JV360的回复,但我认为这不会缩短加载映像的时间,因为映像仍在加载,而且100个映像有100个I/O。我想要的是减少文件系统的I/O数量。你认为我们能做到吗?据我所知。。。但无论你最终做什么,你都应该在后台线程上完成,否则你的应用程序会冻结,直到完成为止……我怀疑JV360的解决方案实际上可能会缩短时间,因为它会同时加载多个图像。这意味着image1的处理可能与image2的I/O同时进行。但是,对于这样的问题,唯一确定的方法就是尝试一下。+1-你肯定需要在后台做这类工作。另外,我也不知道你为什么要设置“减少I/O数量”——一般来说,如果你有100个文件,这意味着100个文件读取。您可以将文件压缩在一起,读取压缩文件(同样在后台),解压缩它,并从原始数据创建UIImage,但这似乎更容易出错,也更困难。这个答案可能最适合你在iOS.wow上的情况,我没有想到。非常感谢范甘迪360和桑卡斯。在我这边尝试过之后,我会回复你们的。目标是什么?更少的电池使用,更短的“时钟”时间,或者更灵敏的用户界面?如果是后者,请尝试加载一个背景线程,甚至可以先加载可见事物的低分辨率缩略图。缩略图会增加总时间,但对用户来说可能实际上更快。