-3

我正在为 imagePicker 演示实现,但它不起作用。当我单击选择图像按钮时,什么也没有发生。我的代码如下:

-(IBAction)pick:(id)sender
{
    ipc=[[UIImagePickerController alloc]init];
    ipc.delegate=self;
    ipc.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    NSLog(@" %@",ipc.sourceType);

    [ipc presentModalViewController:self animated:YES];

}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [[picker parentViewController]dismissModalViewControllerAnimated:YES];
    [picker release];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    img.image=[info objectForKey:UIImagePickerControllerOriginalImage];

    [[picker parentViewController]dismissModalViewControllerAnimated:YES];
    [picker release];
}
4

3 回答 3

1

请更改您的代码

[ipc presentModalViewController:self animated:YES]; 

[self presentModalViewController:ipc animated:YES];
于 2012-06-25T06:48:34.243 回答
1

问题是您正在尝试使用以下方式呈现模态视图:

[ipc presentModalViewController:self 动画:YES];

相反,您应该使用:

[自我presentModalViewController:ipc动画:是];
于 2012-06-25T06:51:12.403 回答
0

这个怎么样:

 -(IBAction)pick:(id)sender
{
       if ([UIImagePickerController isSourceTypeAvailable:
             UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController *imagePicker =
            [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            imagePicker.sourceType =
            UIImagePickerControllerSourceTypeCamera;
            imagePicker.mediaTypes = [NSArray arrayWithObjects:
                                      (NSString *) kUTTypeImage,
                                      nil];
            imagePicker.allowsEditing = NO;
            [self presentModalViewController:imagePicker
                                    animated:YES];

            newMedia = YES;
        } 


 -(void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self.popoverController dismissPopoverAnimated:YES];
    NSString *mediaType = [info
                           objectForKey:UIImagePickerControllerMediaType];

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *image = [info
                          objectForKey:UIImagePickerControllerOriginalImage];

        imageView.image = image;
        if (newMedia)
            UIImageWriteToSavedPhotosAlbum(image,
                                           self,  
                                           @selector(image:finishedSavingWithError:contextInfo:),
                                           nil);
    }
    else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
    {
        // Code here to support video if enabled
    } 
}
于 2012-06-25T06:51:15.007 回答