一旦选择了新的,我试图让我的图像更新。 更新有效但在编辑期间选择新图像时它不会立即显示,我想知道是否有人知道如何修复它。
该应用程序最初是为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];
}
这是updatePhotoButton:
- (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
这是原始问题的更新数据
答案 0 :(得分:0)
iPhone上没有问题的原因是之前的图像在iPhone上不可见,因为您正在使用呈现的视图控制器:图像选择器接管整个屏幕。另一方面,在iPad上,您使用了弹出框,因此主界面仍然可以在弹出窗口后面看到。因此,用户能够看到被点击的图像不是主界面中显示的图像。
但可能会争辩说这没问题。用户尚未实际接受图像,直到图像选择器弹出窗口被解除。你说:
在编辑过程中,它会显示一直存在的图像,因此使用该应用程序的任何人都无法确定是否已添加或更新新选择的图像,这可能会造成混淆。
但是如果用户点击popover以外解除/取消它会怎么样?当然,您不希望图像在主界面中发生变化。因此,您反对的行为是正确的:您抱怨主要界面中的图像在弹出窗口实际被解除之前不会更新,并且确切地说它应该如何。当用户在弹出框中工作时,弹出窗口后面发生的事情是无关紧要的。