【问题标题】:iphone sdk how to take a picture from camera and show the image in same view? [closed]iphone sdk 如何从相机拍照并在同一视图中显示图像? [关闭]
【发布时间】:2012-04-26 10:02:39
【问题描述】:

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

【问题讨论】:

标签: iphone objective-c ios4 uiimagepickercontroller


【解决方案1】:

在单击按钮时调用此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"];
}

【讨论】:

  • 6 秒 :-) 并在我以纵向模式拍照时在 imageview 中以横向显示...抱歉,这是我的最后一个问题..
  • 可以提问.. 6 秒多一点.. 你想要什么风景?我不明白
  • 当我以纵向模式拍照时。图像未以纵向模式显示。它被旋转 90 度并以横向模式显示...
【解决方案2】:

您可以在头文件 (.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];

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多