2013-04-07 22 views
0

我試圖讓我的圖像更新一次新的選擇。 該更新可以正常工作,但在編輯過程中選擇新圖像時不會立即顯示,我想知道是否有人知道如何解決該問題。更新按鈕圖像一次選擇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 

這是一個更新的數據,原來的問題

+0

只有使用[tag:Xcode]標籤才能瞭解關於IDE本身的問題。謝謝! – Undo 2013-04-07 01:46:56

+0

你在哪裏設置圖像?此代碼只將圖像設置爲「choosePhoto」或零。它是否以viewWillAppear(或其他)的方式完成,這就是爲什麼當你回去時只能看到它? – 2013-04-07 02:07:03

+0

嗨邁克,我已經在原始問題中添加了RecipePhotoViewControllers,這是你的意思嗎? – Derek 2013-04-07 02:51:32

回答

0

的原因有沒有問題在iPhone上,前一張圖片在iPhone上不可見,因爲您使用的是呈現的視圖控制器:圖像選擇器會接管整個屏幕。另一方面,在iPad上,您使用的是彈出式窗口,所以主界面在彈出窗口後面仍然可見。因此,用戶能夠看到被點擊的圖像不是在主界面中出現的圖像。

但是可能會認爲這沒有問題。用戶尚未實際接受圖像,直到圖像選擇器彈出消失。你說:

在編輯這表明,在那裏一直以來的形象,所以它可能會造成混淆,就好像新選定的圖像已被添加或更新使用的應用程序任何人都不能肯定。

但是如果用戶在彈出窗口之外點擊以關閉/取消它會怎麼樣?當然,你不希望圖像在主界面中改變。因此,你反對的行爲是正確的:你抱怨主界面中的圖像沒有被更新,直到popover被實際解散,這就是它應該如何。當用戶在popover中工作時,popover後面發生的事情是無關緊要的。

+0

你好馬特,謝謝你的解釋,我明白了。 – Derek 2013-04-08 04:54:19