IOSカメラ管理
カメラの概要
カメラはモバイルデバイスの共通機能の1つです。カメラを使用して画像を撮影し、アプリケーション内で呼び出すことができます。また、カメラの使用はとても簡単です。
実装手順
1、簡単なView based applicationを作成します
2、ViewController.xibにbutton(ボタン)を追加し、そのボタン用にIBActionを作成します
3、image view(画像ビュー)を追加し、imageViewという名前のIBOutletを作成します
4、ViewController.hファイルのコードは次のようになります:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIImagePickerControllerDelegate>
{
UIImagePickerController *imagePicker;
IBOutlet UIImageView *imageView;
}
- (IBAction)showCamera:(id)sender;
@end
5、ViewController.mを次のように修正します:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)showCamera:(id)sender {
imagePicker.allowsEditing = YES;
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else{
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
}
[self presentModalViewController:imagePicker animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
if (image == nil) {
image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
imageView.image = image;
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissModalViewControllerAnimated:YES];
}
@end
出力
このアプリケーションを実行し、「カメラを表示」ボタンをクリックすると、次のような出力が得られます
写真を撮影した後は、移動やズームで画像を編集できます。次のようになります。
