NSData
Web から取得した画像データを含むオブジェクトをキャッシュしています。画像はキャッシュ前に正しく表示されます。キャッシュからデータ オブジェクトを取得すると、データ オブジェクトが同一であっても、そのデータを使用して 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
は等しくなくなりましたが、nil ではなく、適切cacheImage
に表示されます。
編集 - もう少しテストした後、同じ問題が発生することに気付きUIImageJPEGRepresentation
ました。
なぜこれになるのか誰にも分かりますか?ありがとう。