我在表格的表格详细视图上有以下代码,它允许用户拍照或将图像从库加载到 UIImageView。然后这将使用核心数据保存。保存后会回到UITable。奇怪的是,保存后UITable上的图片也从纵向变成了横向。我可以知道我的代码是否有问题。
非常感谢
细节视图.m
- (IBAction)editSaveButtonPressed:(id)sender
{
// If we are adding a new picture (because we didnt pass one from the table) then create an entry
if (!currentPicture)
self.currentPicture = (Pictures *)[NSEntityDescription insertNewObjectForEntityForName:@"Pictures" inManagedObjectContext:self.managedObjectContext];
if (imageField.image)
{
// Resize and save a smaller version for the table
float resize = 74.0;
float actualWidth = imageField.image.size.width;
float actualHeight = imageField.image.size.height;
float divBy, newWidth, newHeight;
if (actualWidth > actualHeight) {
divBy = (actualWidth / resize);
newWidth = resize;
newHeight = (actualHeight / divBy);
} else {
divBy = (actualHeight / resize);
newWidth = (actualWidth / divBy);
newHeight = resize;
}
CGRect rect = CGRectMake(0.0, 0.0, newWidth, newHeight);
UIGraphicsBeginImageContext(rect.size);
[imageField.image drawInRect:rect];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Save the small image version
NSData *smallImageData = UIImagePNGRepresentation(imageField.image);
[self.currentPicture setSmallPicture:smallImageData];
}
// Commit item to core data
NSError *error;
if (![self.managedObjectContext save:&error])
NSLog(@"Failed to add new picture with error: %@", [error domain]);
// Automatically pop to previous view now we're done adding
[self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)imageFromAlbum:(id)sender
{ takePhotoBtn.hidden=YES;
choosePhotoBtn.hidden=YES;
takephoto.hidden=NO;
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:imagePicker animated:YES completion:nil];
}
// Take an image with camera
- (IBAction)imageFromCamera:(id)sender
{ takePhotoBtn.hidden=YES;
choosePhotoBtn.hidden=YES;
takephoto.hidden=NO;
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
[self presentViewController:imagePicker animated:YES completion:nil];
}
表视图.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Get the core data object we need to use to populate this table cell
Pictures *currentCell = [pictureListData objectAtIndex:indexPath.row];
// Fill in the cell contents
cell.textLabel.text = [currentCell title];
cell.detailTextLabel.text = [currentCell desc];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"lightblue.jpg"]];
// If a picture exists then use it
if ([currentCell smallPicture])
{
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
cell.imageView.image = [UIImage imageWithData:[currentCell smallPicture]];
}
tableView.opaque = NO;
[tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"gray.jpg"]]];
return cell;