保存后 UITableViewCell 和 Coredata 上的 UIImage 的方向发生了变化
Posted
技术标签:
【中文标题】保存后 UITableViewCell 和 Coredata 上的 UIImage 的方向发生了变化【英文标题】:Orientation of UIImage on UITableViewCell and Coredata changed after saved 【发布时间】:2012-05-01 06:39:03 【问题描述】:我在表格的表格详细视图上有以下代码,它允许用户从库中拍照或加载图像到 UIImageView。然后这将使用核心数据保存。 保存后会回到UITable。奇怪的是,保存后UITable上的图片也从纵向变成了横向。我可以知道我的代码是否有问题。
非常感谢
DetailView.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];
TableView.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;
【问题讨论】:
它只发生在真正的 iPhone 上。在模拟器中一切都很好。 【参考方案1】:您的图像大小调整未考虑图像旋转。我知道的最简单的解决方案:将图像分配给具有所需大小并使用的 UIImageView。
[imgView.layer renderInContext:context];
这会考虑所有可能的方向。
【讨论】:
【参考方案2】:更改以下部分..
CGRect rect = CGRectMake(0.0, 0.0, newWidth, newHeight);
UIGraphicsBeginImageContext(rect.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];//here you can use your image view instead of self,view...
[imageField.image drawInRect:rect];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
希望对你有所帮助..
【讨论】:
以上是关于保存后 UITableViewCell 和 Coredata 上的 UIImage 的方向发生了变化的主要内容,如果未能解决你的问题,请参考以下文章
将 UITableViewCell 内的 UITextField 保存到 NSMutableArray