-5

我将创建一个按钮和图像视图。我将为按钮创建 IBAction 以打开 UIImagepicker 以拍摄照片。之后我需要图像需要显示在图像视图中。如何获得这个。任何人都可以帮助我。

4

2 回答 2

5

IBAction在单击按钮时调用此方法。它将打开UIImagePickerController以从相机捕获图像。

-(IBAction)openCamera
{
    @try 
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];  
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;  
        picker.delegate = self; 

        [self presentModalViewController:picker animated:YES];
        [picker release];
    }
    @catch (NSException *exception) 
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Camera" message:@"Camera is not available  " delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}

当您完成拍摄图像时,将调用此委托。将该图像存储在您的UIImageView.

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    [picker dismissModalViewControllerAnimated:YES];
    self.imageView.image=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
}
于 2012-04-14T04:37:54.100 回答
1

您可以在头文件 (.h) 中声明 UIImagePickerDelegate,然后在按钮单击事件上编写以下代码。

-(IBAction)YourMethodName_Clicked:(id)sender
{
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])  {
                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                popover = [[UIPopoverController alloc] initWithContentViewController:picker];
                [popover presentPopoverFromRect:CGRectMake(0.0, 0.0, 400.0, 400.0) 
                                         inView:self.view
                       permittedArrowDirections:UIPopoverArrowDirectionAny 
                                       animated:YES];

//                [self presentModalViewController:picker animated:YES];
            }
            else{
                UIAlertView *altnot=[[UIAlertView alloc]initWithTitle:@"Camera Not Available" message:@"Camera Not Available" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                altnot.tag=103;
                [altnot show];
                [altnot release];

            }
}

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


    [picker dismissModalViewControllerAnimated:YES];
    UIImageView *imgview1=[[UIImageView alloc]initWithImage:[info objectForKey:@"UIImagePickerControllerOriginalImage"]];


    imgmain1.frame=CGRectMake(0.0, 0.0, 320.0, 460.0);//set your frame here
    [self.view addSubview:imgmain1];
    [imgmain1 relese];

}
于 2012-04-14T09:43:27.743 回答