我正在尝试一个简单的相机应用程序进行练习。遵循以下示例:iOS 6 App 和iOS 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按钮时,它会打开相机并拍摄图像。是否有可能从应用程序本身做到这一点?对不起,如果这是一个愚蠢的问题。如果我做错了,请指出我。
答案 0 :(得分:2)
是的,请使用UIImagePickerController
class。
单击相机按钮时,显示UIImagePickerController
的实例。此外,由于某些iOS设备(如原始iPad或某些较旧的iPod touch)没有摄像头,因此我不建议使用单独的按钮/方法来拍摄新照片,而不是使用已保存照片中的照片。使用一个按钮并使用类中的可用方法根据设备的功能设置其源类型。它可能需要重新考虑您的应用程序的设计,但它也更有可能通过Apple的UI检查,因为他们可能不会批准具有专门用于拍照的按钮但可以在没有相机的设备上运行的应用程序。
用户拍摄新照片或从相机胶卷中选择照片后,请使用UIImagePickerControllerDelegate
class中的imagePickerController:didFinishPickingMediaWithInfo:
方法将用户选择传递给您的代码。
UIImagePickerController
的实例旨在使用相机时在所有iOS设备上以模态方式呈现。请记住,顾名思义,它们是控制器而不是视图,因此不应将它们用作UIView
的子视图。虽然你可能有可能通过如何呈现它来创造,但你几乎肯定会有一些副作用,你将进一步增加苹果拒绝它的机会。它只是看起来像是从应用程序拍摄照片,但因为你是从你的应用程序呈现控制器,它仍然是它的一部分。 Apple不允许直接访问相机硬件,这正是他们创建这个类的原因。