选择一次更新按钮图像 iPad 不工作
Posted
技术标签:
【中文标题】选择一次更新按钮图像 iPad 不工作【英文标题】:Updating Button image once is selected iPad not working 【发布时间】:2013-04-07 00:51:59 【问题描述】:一旦选择了新的,我正在尝试更新我的图像。 该更新有效,但在编辑期间选择新图像时不会立即显示,我想知道是否有人知道如何修复它。
该应用程序最初是为 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
这是原始问题的更新数据
【问题讨论】:
仅使用xcode 标签来回答有关 IDE 本身的问题。谢谢! 你在哪里设置图像?此代码仅将图像设置为“choosePhoto”或 nil。它是在 viewWillAppear (或类似的)中完成的,这就是为什么你只有在返回时才能看到它? 嗨,迈克,我在原来的问题中添加了 RecipePhotoViewControllers,这是你的意思吗? 【参考方案1】:在 iPhone 上没有问题的原因是之前的图像在 iPhone 上不可见,因为您使用的是呈现的视图控制器:图像选择器占据了整个屏幕。另一方面,在 iPad 上,您使用的是弹出框,因此在弹出框后面仍然可以看到主界面。因此,用户可以看到被点击的图像不是回到主界面中的图像。
但可能会争辩说这没有问题。用户实际上还没有接受图像,直到图像选择器弹出框被解除。你说:
在编辑过程中,它会显示一直存在的图像,因此可能会造成混淆,因为使用该应用程序的任何人都无法确定新选择的图像是否已添加或更新。
但是如果用户在弹出框外点击以关闭/取消它怎么办?当然,您不希望图像在主界面中发生变化。因此,您反对的行为是正确的:您抱怨主界面中的图像在弹出框实际关闭之前不会更新,而这正是它应该的样子。当用户在弹窗中工作时,弹窗后面发生的事情是无关紧要的。
【讨论】:
你好,马特,谢谢你的解释,我明白了。以上是关于选择一次更新按钮图像 iPad 不工作的主要内容,如果未能解决你的问题,请参考以下文章