私はインターネット上のあらゆる場所を見てきましたが、私にとって単一の解決策が見つかりませんでした. 私のアプリでは、テーブルビューがあり、ユーザーはそこに独自のセルを追加します。詳細ビューには、UIImageView と写真を撮れる場所があります。その画像をアプリのどこかに保存できるようにする必要があり、保存した後、後でセルをクリックすると、しばらく前に撮った画像が表示されます。
テーブル ビュー セルとテーブルビューのコードは次のとおりです。
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) // Don't move the first row
return NO;
return YES;
}
-(void)tableView:(UITableView *)tableView setEditing:(BOOL)editing animated:(BOOL) animated {
[super setEditing:editing animated:animated];
[tableView setEditing:editing animated:animated];
NSLog(@"EDIT BUTTON WILL BEGIN EDITING");
if (editing) {
[self.tableView setEditing:YES animated:YES];
} else {
[self.tableView setEditing:NO animated:YES];
}
[tableView reloadData];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *context = [self managedObjectContext];
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete object from database
[context deleteObject:[self.devices objectAtIndex:indexPath.row]];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
return;
}
// Remove device from table view
[WTStatusBar setStatusText:@"Removed" timeout:0.7 animated:YES];
[self.devices removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _devices.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[cell.textLabel setTextColor:[UIColor grayColor]];
[cell.textLabel setHighlightedTextColor:[UIColor whiteColor]];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@", [device valueForKey:@"name"]]];
[cell.detailTextLabel setText:[device valueForKey:@"company"]];
[cell.textLabel setBackgroundColor:[UIColor clearColor]];
[cell.detailTextLabel setBackgroundColor:[UIColor clearColor]];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
cell.backgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"TableViewCell.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
return cell;
}
- (NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
id ob = [_devices objectAtIndex:destinationIndexPath.row];
[_devices replaceObjectAtIndex:destinationIndexPath.row withObject:[_devices objectAtIndex:sourceIndexPath.row]];
[_devices replaceObjectAtIndex:sourceIndexPath.row withObject:ob];
}
テーブルビューに保存するためのコードは次のとおりです。
- (IBAction)save:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];
UIImage *image = resultImage.image;
NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@KYRO Receipts Images/Barcode.png", resultImage]]; //add our image to the path
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)
NSLog(@"image saved");
if (self.device) {
// Update existing device
[self.device setValue:self.nameOfItem.text forKey:@"name"];
[self.device setValue:self.dateOfPurchase.text forKey:@"date"];
[self.device setValue:self.companyOfItem.text forKey:@"company"];
} else {
//
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Receipt" inManagedObjectContext:context];
[newDevice setValue:self.nameOfItem.text forKey:@"name"];
[newDevice setValue:self.dateOfPurchase.text forKey:@"date"];
[newDevice setValue:self.companyOfItem.text forKey:@"company"];
}
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
[WTStatusBar setStatusText:@"Saving data..." animated:YES];
[self performSelector:@selector(setTextStatusProgress3) withObject:nil afterDelay:0.1];
[self.navigationController popViewControllerAnimated:YES];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}