gpt4 book ai didi

ios - 从 iOS CoreData 获取图像

转载 作者:行者123 更新时间:2023-11-29 04:13:36 25 4
gpt4 key购买 nike

我有一个使用 NSData 属性 imageData 定义的核心数据实体。我在实体上为 image 设置了 setter 和 getter。

setter,setImage,只需调用[self setImageData:UIImagePNGRepresentation(img)];getter image 仅返回 [UIImage imageWithData:[self imageData]];

当我创建新实体并且获取的结果 Controller 加载新表行时,图像始终为 null。但是,如果我点击该行转到下一个 View Controller ,图像就会正常显示

如何才能使其在保存发生并且 fetchResultsController 更新为新行后立即显示图像。

编辑:添加代码

图像片段的核心数据 header 和 getter/setter

.h
@property (nonatomic, retain) NSData * imageData;

@property (nonatomic, retain) UIImage *image;

.m
- (void)setImage:(UIImage *)image
{
NSData *data = UIImagePNGRepresentation(image);
[self setImageData:data];
}

- (UIImage *)image
{
return [UIImage imageWithData:[self imageData]];
}

表格 View 代码

- (NSFetchedResultsController *)fetchedResultsController {

if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}

[NSFetchedResultsController deleteCacheWithName:@"Artists"];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ArtistModel" inManagedObjectContext:[[BKDataManager sharedInstance] mainObjectContext]];
[fetchRequest setEntity:entity];

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

[fetchRequest setFetchBatchSize:20];

_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[[BKDataManager sharedInstance] mainObjectContext] sectionNameKeyPath:nil cacheName:@"Artists"];

_fetchedResultsController.delegate = self;

return _fetchedResultsController;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ArtistTableViewCell *cell = (ArtistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ArtistsTableViewCell"];

if(!cell) {
cell = (ArtistTableViewCell *)[[[UINib nibWithNibName:@"ArtistTableViewCell" bundle:nil] instantiateWithOwner:nil options:nil] objectAtIndex:0];
}

ArtistModel *row = [[self fetchedResultsController] objectAtIndexPath:indexPath];

[[cell artistNameLabel] setText:[row name]];
[[cell artistImageView] setImage:[row image]]; // This is where it pulls from the artist model

return cell;
}

保存到核心数据位

- (void)addArtistWithName:(NSString *)name confirmed:(BOOL)confirmed managedContext:(NSManagedObjectContext *)managedContext success:(BKClientIdResult)success failure:(BKClientError)failure
{
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
name, @"name",
[NSNumber numberWithBool:confirmed], @"confirmed"
, nil];

[[BKClient sharedInstance] postPath:@"/users/artists" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
if([[responseObject objectForKey:@"status"] boolValue]) {
NSDictionary *result = [responseObject valueForKeyPath:@"payload.artist"];

ArtistModel *artistModel = (ArtistModel *)[NSEntityDescription insertNewObjectForEntityForName:@"ArtistModel" inManagedObjectContext:managedContext];

[artistModel setName:[result objectForKey:@"name"]];

NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[result objectForKey:@"image"]]];

AFImageRequestOperation *op = [AFImageRequestOperation imageRequestOperationWithRequest:imageRequest imageProcessingBlock:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
[artistModel setImage:image];

[managedContext save:nil];

if(success) {
success(artistModel);
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
// TODO FIX
}];

[op start];
} else {
if(failure) {
failure([NSError errorWithDomain:@"com.nf" code:0 userInfo:[responseObject objectForKey:@"error"]]);
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if(failure) {
failure(error);
}
}];
}

最佳答案

根据您的问题,刷新数据的最简单方法是执行新的提取(通过NSFetchedResultsController)并在完成保存后重新加载 TableView 。

NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
// error handling here...
}

[yourTableView reloadData];

或者,您可以考虑为您的 NSFetchedResultsController 设置委托(delegate)。欲了解更多信息,请查看此处NSFetchedResultsControllerDelegate doccore-data-tutorial-how-to-use-nsfetchedresultscontroller .

设置此委托(delegate)后,您可以监听托管上下文的更改并正确响应 TableView 修改(添加、删除、更新部分和行)。

附言如果您提供一些代码,我们将能够为您提供更详细的帮助。

关于ios - 从 iOS CoreData 获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14026734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com