2011-02-24 110 views
0

我用我的UIImagePickerController委託下面的代碼:的UIImage不從攝像頭顯示,剛剛從照片庫

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    UIImage *image = (UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage]; 
    [self.personPhotoButton setBackgroundImage:image forState:UIControlStateNormal]; 

    [self dismissModalViewControllerAnimated:YES]; 
} 

它完美如果圖像是從用戶的照片庫,但如果它來自相機,沒有圖像出現在按鈕中。沒有任何錯誤或崩潰,它只是不起作用(按鈕背景保持不變)。當我在info上使用NSLog時,它顯示UIImagePickerControllerOriginalImage中有一個圖像對象,但它不會顯示。我嘗試調整它的大小,但這並沒有改變任何東西。

有什麼想法?

編輯:我能八九不離十得到它的工作通過添加下面的上述方法:

UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); 

然後,我有這樣的方法:

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { 
    [self.personPhotoButton setBackgroundImage:image forState:UIControlStateNormal]; 
} 

所以,是否需要保存照片才能顯示?除非明確保存到相冊中,否則我不能顯示UIImage嗎?理想情況下,我寧願不必這樣做,但似乎這是我可能必須走的路線。我也會嘗試將它保存到磁盤(而不是照片庫),也許這將工作。

回答

2

事實證明,問題出在我撥打dismissModalViewControllerAnimated:的地方。如果我將它移動到方法的頂部,一切都按預期工作:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    [self dismissModalViewControllerAnimated:YES]; 

    UIImage *image = (UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage]; 
    [self.personPhotoButton setBackgroundImage:image forState:UIControlStateNormal]; 
} 
0

也許你應該在添加背景之前保留圖像。

+0

是的,我試過,沒有骰子:( – 2011-02-25 02:12:54