iphone sdk如何从相机拍照并在同一视图中显示图像?

时间:2012-04-14 04:29:18

标签: iphone objective-c ios4 uiimagepickercontroller

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

2 个答案:

答案 0 :(得分: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"];
}

答案 1 :(得分: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];

}