【问题标题】:Accessing the camera from the app itself从应用程序本身访问相机
【发布时间】:2012-10-25 10:53:38
【问题描述】:

我正在尝试一个简单的相机应用程序进行练习。遵循这些示例:iOS 6 AppiOS 4 App

我的代码如下所示:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
    UIButton *button;
    UIImageView *imageView;
}
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
- (IBAction)useCamera;
- (IBAction)useCameraRoll;
@end

ViewController.m

#import "ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>

@interface ViewController ()

@end

@implementation ViewController

@synthesize imageView;


- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // After saving iamge, dismiss camera
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    UIAlertView *alert;

    // Unable to save the image
    if (error)
        alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                           message:@"Unable to save image to Photo Album."
                                          delegate:self cancelButtonTitle:@"Ok"
                                 otherButtonTitles:nil];
    else // All is well
        alert = [[UIAlertView alloc] initWithTitle:@"Success"
                                           message:@"Image saved to Photo Album."
                                          delegate:self cancelButtonTitle:@"Ok"
                                 otherButtonTitles:nil];


    [alert show];
}

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Access the uncropped image from info dictionary
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    // Save image
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

}

- (id)init
{
    if (self = [super init])
    {
        self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
        self.view.backgroundColor = [UIColor grayColor];

        // Button to activate camera
        button = [[UIButton alloc] initWithFrame:CGRectMake(80, 55, 162, 53)];
        [button setBackgroundImage:[UIImage imageNamed:@"Camera.png"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
        [self.view addSubview:button];
    }

    return self;  
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)useCamera
{
    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 presentViewController:imagePicker animated:YES completion:nil];

    }
}

- (IBAction)useCameraRoll
{
    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    {
        UIImagePickerController *imagePicker =
        [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType =
        UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:
                                  (NSString *) kUTTypeImage,
                                  nil];
        imagePicker.allowsEditing = NO;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }
}

@end

我的问题我喜欢从应用程序本身访问相机。发生的情况是,当我单击 useCamera 按钮时,它会打开相机并拍摄图像。是否有可能从应用程序本身做到这一点?对不起,如果这是一个愚蠢的问题。如果我做错了什么,请指出我。

【问题讨论】:

  • 嗨,你想要做的是在你的应用程序中打开 uiimagepicker 吗?保存导航控制器(例如)和 tabbarcontroller(例如)?
  • 是的...它似乎离开了应用程序进行拍照...在保存导航控制器部分,不...
  • 我不确定,但不是 presentViewController 你可以让 [self.view addsubview:youpicker.view] no 吗?所以你可以自定义你的选择器框架等......

标签: iphone ios ios6 uiimagepickercontroller


【解决方案1】:

是的,使用UIImagePickerController class

当点击相机按钮时,呈现UIImagePickerController 的实例。此外,由于某些 iOS 设备(例如原始 iPad 或某些较旧的 iPod touch)没有摄像头,因此我不建议使用单独的按钮/方法来拍摄新照片,而不是使用已保存照片中的一个。使用一个按钮并使用类中可用的方法根据设备的功能设置其源类型。这可能需要重新考虑您的应用的设计,但它也更有可能通过 Apple 的 UI 检查,因为他们可能不会批准具有专门用于拍照的按钮但可以在没有相机的设备上运行的应用。

一旦用户拍摄了一张新照片或从相机胶卷中选择了一张照片,请使用 UIImagePickerControllerDelegate class 中的 imagePickerController:didFinishPickingMediaWithInfo: 方法将用户的选择传递给您的代码。

UIImagePickerController 的实例设计为在使用相机时以模态方式呈现在所有 iOS 设备上。请记住,顾名思义,它们是控制器而不是视图,因此不应将它们用作UIView 的子视图。虽然您可能会在如何呈现它时发挥创意,但您几乎肯定会产生一些副作用,并且您会进一步增加 Apple 拒绝它的机会。它似乎只是从应用程序中拿出来拍照,但因为你是从你的应用程序中展示控制器,它仍然是它的一部分。 Apple 不允许直接访问相机硬件,这正是他们创建此类的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多