これは、このアプリの実行でエントリが作成された場合にのみ発生します。Entry が以前に作成されている場合、画像は正常にフェッチされます。
このコードはバックグラウンド スレッドを使用しなくても問題なく動作するため、問題の一部であると思われます。ここに私が持っているコードがあります:
NSMutableDictionary *thumbnails = [[NSMutableDictionary alloc] init];
dispatch_queue_t thumbnailSetupQueue = dispatch_queue_create("com.App.SetupTimelineThumbnails", NULL);
dispatch_async(cellSetupQueue, ^{
NSManagedObjectContext *newMoc = [[NSManagedObjectContext alloc] init];
NSPersistentStoreCoordinator *coordinator = [NSManagedObjectContext contextForCurrentThread].persistentStoreCoordinator;
[newMoc setPersistentStoreCoordinator:coordinator];
NSNotificationCenter *notify = [NSNotificationCenter defaultCenter];
[notify addObserver:self
selector:@selector(mergeChanges:)
name:NSManagedObjectContextDidSaveNotification
object:newMoc];
Media *media = [localEntry.media anyObject];
UIImage *image = [media getThumbnail];
NSLog(@"image: %@", image);
[[NSNotificationCenter defaultCenter] removeObserver:self];
});
dispatch_release(cellSetupQueue);
それで
-(UIImage *)getThumbnail {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:@"%@-%@.jpg",
self.mediaID,
THUMBNAIL_FILENAME]];
UIImage *thumbnail = [UIImage imageWithContentsOfFile:fullPath];
NSLog(@"correct size thumbnail: %@", correctSizeThumbnail);
return correctSizeThumbnail;
}
getThumbnailWithSave の NSLog は UIImage として返され、他の NSLog は nil として返されます。