Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.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
Ios 缓存的NSData对象可以';检索后不能使用_Ios_Caching_Uiimage_Nsdata - Fatal编程技术网

Ios 缓存的NSData对象可以';检索后不能使用

Ios 缓存的NSData对象可以';检索后不能使用,ios,caching,uiimage,nsdata,Ios,Caching,Uiimage,Nsdata,我正在缓存包含从web检索的图像数据的NSData对象。缓存前图像显示正确。当我从缓存中检索数据对象时,即使数据对象相同,数据也不能再用于创建UIImage 请参阅下面我的代码的相关片段 NSData *webData= [NSData dataWithContentsOfURL:webPath]; //retrieve from web UIImage *webImage = [UIImage imageWithData:webData]; //works fine [webData wri

我正在缓存包含从web检索的图像数据的
NSData
对象。缓存前图像显示正确。当我从缓存中检索数据对象时,即使数据对象相同,数据也不能再用于创建UIImage

请参阅下面我的代码的相关片段

NSData *webData= [NSData dataWithContentsOfURL:webPath]; //retrieve from web
UIImage *webImage = [UIImage imageWithData:webData]; //works fine

[webData writeToURL:filePath atomically:YES]; //cache
NSData *cacheData = [NSData dataWithContentsOfURL:filePath]; //get from cache
if ([cacheData isEqualToData:webData]) NSLog(@"Equal"); //Data objects are equal

UIImage *cacheImage = [UIImage imageWithData:cacheData]; //cacheImage is nil
我可以通过更改将数据存储到缓存的方式来解决此问题

NSData *temp = UIImageJPEGRepresentation(webImage, 1.0):
[temp writeToURL:filePath atomically:YES];
现在,
webData
cacheData
不再相等,但是
cacheImage
不是零并正确显示


编辑-经过一点测试后,我意识到使用
uiimagejpegresentation
也会遇到同样的问题

有人知道为什么会这样吗?
谢谢。

发现问题是我在视图控制器完全加载之前尝试执行所有这些操作。

UIImageJPEGresentation(webImage,1.0)
没有压缩图像数据。您可以记录writeToURL的结果吗?还记录缓存数据吗?我的猜测是写/读失败了。cacheData为零,因此它不等于webDataI记录了结果,缓存工作正常。webData和cacheData相等,而不是为零。