一旦选择了新的,我正在尝试更新我的图像。更新有效,但在编辑期间选择新图像时不会立即显示,我想知道是否有人知道如何修复它。
该应用程序最初是为 iPhone 创建的,此功能运行良好,即使在 iPhone 编辑期间选择新图像,图像也会立即更新,但是当我在 iPad 上使用它时,图像在编辑过程中不会改变,但一旦我完成编辑并返回,我可以看到图像发生了变化。
我正在从开发人员中心制作核心数据配方示例代码以在 iPad 上运行。
这是我用于选择新的 Image PhotoTapped1 按钮的代码,已更改为可与 iPad 一起使用:
- (IBAction)photoTapped1 {
if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
// If in editing state, then display an image picker; if not, create and push a photo view controller.
if (self.editing) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:nil];
[imagePicker release];
} else {
RecipePhotoViewController *recipePhotoViewController = [[RecipePhotoViewController alloc] init];
recipePhotoViewController.hidesBottomBarWhenPushed = YES;
recipePhotoViewController.recipe = recipe;
[self.navigationController pushViewController:recipePhotoViewController animated:YES];
[recipePhotoViewController release];
}
}else{
if (self.editing){
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[self.popover presentPopoverFromRect:CGRectMake(0, 0, 400, 400) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
self.popover.delegate = self;
[popover release];
}else{
RecipePhotoViewController *recipePhotoViewController = [[RecipePhotoViewController alloc] init];
recipePhotoViewController.hidesBottomBarWhenPushed = YES;
recipePhotoViewController.recipe = recipe;
[self.navigationController pushViewController:recipePhotoViewController animated:YES];
[recipePhotoViewController release];
}}}
这工作正常,图像更新,但不是在编辑期间。在编辑过程中,它会显示一直存在的图像,因此可能会令人困惑,因为使用该应用程序的任何人都无法确定新选择的图像是否已添加或更新。
以下是 imagePickerController 和 updateButton 的代码:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo {
// Delete any existing image.
NSManagedObject *oldImage = recipe.image;
if (oldImage != nil) {
[recipe.managedObjectContext deleteObject:oldImage];
}
// Create an image object for the new image.
NSManagedObject *image = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:recipe.managedObjectContext];
recipe.image = image;
// Set the image for the image managed object.
[image setValue:selectedImage forKey:@"image"];
// Create a thumbnail version of the image for the recipe object.
CGSize size = selectedImage.size;
CGFloat ratio = 0;
if (size.width > size.height) {
ratio = 44.0 / size.width;
} else {
ratio = 44.0 / size.height;
}
CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);
UIGraphicsBeginImageContext(rect.size);
[selectedImage drawInRect:rect];
recipe.thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self dismissModalViewControllerAnimated:YES];}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion: nil];
}
这是更新照片按钮:
- (void)updatePhotoButton {
/* How to present the photo button depends on the editing state and whether the recipe has a thumbnail image.
* If the recipe has a thumbnail, set the button's highlighted state to the same as the editing state (it's highlighted if editing).
* If the recipe doesn't have a thumbnail, then: if editing, enable the button and show an image that says "Choose Photo" or similar; if not editing then disable the button and show nothing.
*/
BOOL editing = self.editing;
if (recipe.thumbnailImage != nil) {
photoButton.highlighted = editing;
} else {
photoButton.enabled = editing;
if (editing) {
[photoButton setImage:[UIImage imageNamed:@"choosePhoto.png"] forState:UIControlStateNormal];
} else {
[photoButton setImage:nil forState:UIControlStateNormal];
}
}
}
有谁知道会出现什么问题?我知道 UIImagePickerControll 必须与 UIPopoverController 一起使用才能在 iPad 上工作,但正在更新 -(IBAction)PhotoTapped1 代码。
不知道从这里去哪里。
谢谢你。
更新问题..
PhotoViewController.h
@class Recipe;
@interface RecipePhotoViewController : UIViewController {
@private
Recipe *recipe;
UIImageView *imageView;
}
@property(nonatomic, retain) Recipe *recipe;
@property(nonatomic, retain) UIImageView *imageView;
@end
RecipePhotoViewController.m
#import "RecipePhotoViewController.h"
#import "Recipe.h"
@implementation RecipePhotoViewController
@synthesize recipe;
@synthesize imageView;
- (void)loadView {
self.title = @"Photo";
imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.backgroundColor = [UIColor blackColor];
self.view = imageView;
}
- (void)viewWillAppear:(BOOL)animated {
imageView.image = [recipe.image valueForKey:@"image"];
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)dealloc {
[imageView release];
[recipe release];
[super dealloc];
}
@end
这是原始问题的更新数据