IOS相機管理(lǐ)
相機簡介
相機是移動設備的共同特點之一,我們能(néng)夠使用(yòng)相機拍(pāi)攝圖片,并在應用(yòng)程序裏調用(yòng)它,而且相機的使用(yòng)很(hěn)簡單。
實例步驟
1、創建一個簡單的View based application
2、在ViewController.xib中添加一個button (按鈕),并爲該按鈕創建IBAction
3、添加一個 image view (圖像視(shì)圖),并創建一個名爲imageView的IBOutlet
4、ViewController.h文(wén)件代碼如下(xià)所示:
#import
@interface ViewController : UIViewController
{
UIImagePickerController *imagePicker;
IBOutlet UIImageView *imageView;
}
- (IBAction)showCamera:(id)sender;
@end
5、修改ViewController.m,如下(xià)所示:
#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
網站(zhàn)建設開(kāi)發|APP設計(jì)開(kāi)發|小(xiǎo)程序建設開(kāi)發