1

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

我的代码如下所示:

视图控制器.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

视图控制器.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 按钮时,它会打开相机并拍摄图像。是否有可能从应用程序本身做到这一点?对不起,如果这是一个愚蠢的问题。如果我做错了什么,请指出我。

4

1 回答 1

2

是的,使用UIImagePickerController

When the camera button is clicked, present an instance of UIImagePickerController. Also, since some iOS devices like the original iPad or some older iPod touches don't have cameras, I wouldn't recommend using separate buttons/methods for taking a new picture vs using one from the saved photos. Use one button and set its source type based on the device's capabilities using the available methods in the class. It may require rethinking your app's design, but it's also more likely to pass Apple's UI inspection, as they may not approve an app that has a button exclusively for taking a picture but can run on a device without a camera.

Once the user has either taken a new photo or chosen a photo from either the camera roll, use the imagePickerController:didFinishPickingMediaWithInfo: method from the UIImagePickerControllerDelegate class to pass the user's choice to your code.

Instances of UIImagePickerController are designed to be presented modally on all iOS devices when using the camera. Remember, as the name suggests, they are controllers and NOT views, so they shouldn't be used as subviews of UIView. While it may be possible to get creative with how you present it, you'll almost certainly have some side-effects and you'll further increase the chance that Apple will reject it. It only SEEMS like it's getting out the app to take the photo, but because you're presenting the controller from your app, it's still very much a part of it. Apple doesn't allow direct access to the camera hardware, which is exactly why they've created this class.

于 2012-11-05T16:27:46.917 回答