我試圖讓我的圖像更新一次新的選擇。 該更新可以正常工作,但在編輯過程中選擇新圖像時不會立即顯示,我想知道是否有人知道如何解決該問題。更新按鈕圖像一次選擇iPad不能正常工作
該應用程序最初是爲iPhone創建的,此功能工作正常,即使在編輯iPhone時選擇了新圖像,圖像也會立即更新,但是當我在iPad上使用它時,圖像不會在更改期間更改編輯,但一旦我完成編輯並返回,我可以看到圖像正在改變。
我正在從開發人員中心製作核心數據配方示例代碼,以在iPad上工作。
這是我選擇新的圖像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
這是一個更新的數據,原來的問題
只有使用[tag:Xcode]標籤才能瞭解關於IDE本身的問題。謝謝! – Undo 2013-04-07 01:46:56
你在哪裏設置圖像?此代碼只將圖像設置爲「choosePhoto」或零。它是否以viewWillAppear(或其他)的方式完成,這就是爲什麼當你回去時只能看到它? – 2013-04-07 02:07:03
嗨邁克,我已經在原始問題中添加了RecipePhotoViewControllers,這是你的意思嗎? – Derek 2013-04-07 02:51:32