你好,歡迎來到IOS教程網

 Ios教程網 >> IOS基礎知識 >> IOS入門簡介 >> iOS相機管理

iOS相機管理

編輯:IOS入門簡介

IOS相機管理


相機簡介

相機是移動設備的共同特點之一,我們能夠使用相機拍攝圖片,並在應用程序裡調用它,而且相機的使用很簡單。

實例步驟

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

輸出

運行該應用程序並單擊顯示相機按鈕時,我們就會獲得下面的輸出

camera_Output1

只要拍照之後,就可以通過移動和縮放對圖片進行編輯,如下所示。

camera_Output2

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved