我有一个应用程序,我在其中存储textLabel和detailTextLabel并将其加载到带有原型单元格的UITableView。一切都运作良好。我拍了一张照片并将其存储在我的文档目录中并检索它并在UIImageView中显示它。再好。 我试图将图像保存到我的coreData并在TableView中显示它,但每个单元格的图片不同。 这是我将图像数据保存到coreData的代码:
NSString *stringOneb = fileName2;
NSString *stringTwob = [stringOneb stringByAppendingString:@","];
NSString *stringFour = [stringTwob stringByAppendingString:yourName];
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory1 = [paths1 objectAtIndex:0];
NSString *slash3 = [documentsDirectory1 stringByAppendingPathComponent:@"/Images"];
NSString *fullPath1 = [slash3 stringByAppendingPathComponent:stringFour];
fullPath1 = [fullPath1 stringByAppendingFormat:@".png"];
//Now load the image at fullPath and install it into our image view's image property.
if (![[NSFileManager defaultManager] fileExistsAtPath:fullPath1]){
}
else {
NSURL *url = [NSURL URLWithString:fullPath1];
NSData *data5 = [[NSData alloc] initWithContentsOfURL:url];
UIImage *cellimage = [UIImage imageWithData:data5];
photoView2.image = [UIImage imageWithContentsOfFile: fullPath1];
myimageData = [NSData dataWithData:UIImagePNGRepresentation(cellimage)];
}
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Cell" inManagedObjectContext:context];
[newDevice setValue:yourName forKey:@"name"];
[newDevice setValue:Seven forKey:@"winloss"];
[newDevice setValue:myimageData forKey:@"image"];
NSError *error7 = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error7, [error7 localizedDescription]);
}
}
else {
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *selectedDevice = [self.tableDataMainView objectAtIndex:indexNumber ];
[selectedDevice setValue:yourName forKey:@"name"];
[selectedDevice setValue:Seven forKey:@"winloss"];
[selectedDevice setValue:myimageData forKey:@"image"];
NSError *error7 = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error7, [error7 localizedDescription]);
}
我没有收到任何错误,因此我假设图像已被保存。
这就是我尝试在TableView单元格中检索图像的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSManagedObject *device = [self.tableDataMainView objectAtIndex:indexPath.row];
[cell.textLabel setText:[device valueForKey:@"name"]];
cell.detailTextLabel.frame = CGRectMake(16, 35, 200, 65);
[cell.detailTextLabel setText:[device valueForKey:@"winloss"]];
[cell.imageView setImage:[device valueForKey:@"image"]];
[cell.textLabel setTextColor:[UIColor whiteColor]];
[cell.detailTextLabel setTextColor:[UIColor whiteColor]];
return cell;
}
如果我在我的主要包中引用一个图像,它会显示但是当我引用valueForKey时:@“image”我什么都没得到。
不确定我做错了什么,有人可以提供帮助。感谢
答案 0 :(得分:0)