2015-05-19 72 views
-3
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init] ; 
[imagePicker setAllowsEditing:YES] ; 
[imagePicker setDelegate:self] ; 
[imagePicker setMediaTypes:[NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil]] ; 
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary] ; 
[self presentViewController:imagePicker animated:YES completion:nil] ; 
+2

用適當的格式發佈您的問題。 –

+0

您可以添加輸出控制檯嗎?什麼是錯誤? – thedjnivek

回答

1

第1步:添加委託

@interface APPViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> 

第2步:初始化UIImagePicker上點擊按鈕動作

- (IBAction)selectPhoto:(UIButton *)sender { 

    UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
    picker.delegate = self; 
    picker.allowsEditing = YES; 
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    [self presentViewController:picker animated:YES completion:NULL];  
} 

第3步:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage]; 
    self.imageView.image = chosenImage; 
    [picker dismissViewControllerAnimated:YES completion:NULL]; 
} 

第4步:關閉控制器

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{  
     [picker dismissViewControllerAnimated:YES completion:NULL];  
} 

注意:您發佈的細節描述問題

0

您正在使用的UIImagePickerController作爲變量而不是作爲一個屬性或特性,這意味着將在您的函數結束時被釋放。將您的UIImagePickerController設置爲屬性或屬性,然後該委託將工作。

0

如果您使用的是UIPopoverPresentationController,請確保在呈現imagePicker之前關閉彈出窗口。否則,它不會顯示。

相關問題