2011-11-22 78 views
2

我寫了最簡單的iPhone應用程序。它有一個單一的視圖窗口。該主視圖包含圖像視圖和2個按鈕,分別標記爲「圖庫中的圖片」和「拍照」。應用程序會根據所選按鈕生成圖像,並將其作爲UIImageView的圖像。以獲取圖像的代碼如下:由UIImagePickerViewController返回的圖像不顯示

- (void) onCapturePicture:(BOOL)fromLibrary 
{ 
    UIImagePickerController *controller = [[UIImagePickerController alloc] init]; 
    [controller setDelegate: self]; 
    [controller setSourceType: fromLibrary ? UIImagePickerControllerSourceTypePhotoLibrary : UIImagePickerControllerSourceTypeCamera]; 
    [controller setAllowsEditing: YES]; 
    [self presentModalViewController: controller animated: YES]; 
    [self setImagePickerController: controller]; 
} 

在圖像被返回並分配給UIImageView控制委託函數如下:

- (void) imagePickerController:(UIImagePickerController *)picker 
didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIImage *pickedImage = [info objectForKey: UIImagePickerControllerEditedImage]; 

    [_pictureView setImage: pickedImage]; 
    [[self view] setNeedsDisplay]; 
    [self dismissModalViewControllerAnimated: YES]; 
} 

我也試圖與UIImagePickerControllerOriginalImage與相同的結果,即:當照片來自照片庫時,它會顯示爲UIImageView中的圖像。使用相機拍攝照片時,即使指向返回圖像的指針不爲零,它也完全不顯示。如果我使用

- (void) imagePickerController:(UIImagePickerController *)picker 
     didFinishPickingImage:(UIImage *)image 
        editingInfo:(NSDictionary *)editingInfo 

代替,一切正常,但文件表明它是過時的,我想這可能是對被拒絕我的應用程序的一個原因。

另外,我有2個與主題相關的其他問題:

  1. UIPickerController需要很長的時間來顯示第一次採摘從照片庫中的圖片時,它的調用(如11秒) 。有什麼辦法可以減少這種加載時間,並帶來一些竅門? (在空閒時間靜靜地預加載什麼)?

  2. 只要照相機打開,應用程序就會收到內存警告(接收到的內存警告,級別= 1)。有沒有辦法避免它,或者我做錯了什麼?這是一個帶2個按鈕的普通香草應用程序。基本上我爲這個應用程序輸入的代碼90%列在這篇文章中。只要打開相機就會導致這種情況發生。我怎樣才能避免這個問題?我在UIImagePickerController上閱讀了其他有關內存泄漏的帖子,但這不適用於這種情況。第一次執行代碼以獲取相機的圖片時,我收到內存警告。

在此先感謝。

回答

1

您可能需要更改這些行:

[[self view] setNeedsDisplay]; 
[self dismissModalViewControllerAnimated: YES]; 

到:

[self dismissViewControllerAnimated:YES completion:nil]; 

正如我不相信你需要刷新顯示。我建議你改變解僱方法,只是因爲我測試了它,它的工作原理。祝你好運。