弹出视图选择图像

时间:2012-06-12 18:33:28

标签: ios uiview uiimagepickercontroller

我想创建一个如下图所示的示例,而是从照片库中选择图像或用相机拍照(如facebook app)。这个视图是内置的还是我需要创建自定义视图?

Example

1 个答案:

答案 0 :(得分:3)

所以你想要一张UIActionSheet?

这样做:

UIActionSheet *actionSheet = [[UIActionSheet alloc] 
                                  initWithTitle:@"What do you want to do?" 
                                  delegate:self 
                                  cancelButtonTitle:@"Cancel" 
                                  destructiveButtonTitle:nil 
                                  otherButtonTitles:@"Camera", @"Photos", nil];
    [actionSheet showInView:self.view];
    [actionSheet release];

然后在clickedButtonAtIndex中执行以下操作:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;

    if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Camera"]) {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
    } 
    else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Photos"]) {
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
   [self presentModalViewController:picker animated:YES];
   [picker release];
}