这仅在此应用程序运行中创建条目时发生。如果之前创建了条目,它会很好地获取图像。
这段代码在不使用后台线程的情况下工作正常,所以它让我相信它是问题的一部分。这是我的代码:
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 形式返回。