iOS Programming Camera 1,iosprogramming

來源:互聯網
上載者:User

iOS Programming Camera 1,iosprogramming

 iOS Programming Camera  1

1 Displaying Images and UIImageView

1.1  put an instance of UIImageView on the screen.

Then drag an instance of UIImageView onto the view and position it below the label.

A UIImageView displays an image according to its contentMode property.

UIImageView根據它的contentMode 屬性來展示圖片。

This property determines where to position and how to resize the content within the image view's frame.

這個屬性決定了在哪兒和在image View的架構內怎樣重新顯示尺寸的內容。

UIImageView's default value for contentMode is UIViewContentModeScaleToFill, which will adjust the image to exactly match the bounds of the image view.

預設是UIViewContentModeScaleToFill。

If you keep the default, an image taken by the camera will be contorted to fit into the square UIImageView.

如果你採用預設設定,你的圖片可能被扭曲以來適應方形的UIImageView。

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

1.2 Adding a camera button 

Now you need a button to initiate the photo-taking process.

現在你需要一個button 來初始化photo-taking process。 

Instead, you will create an instance of UIToolbar and place it at the bottom of BNRDetailViewController's view.

你需要建立一個UIToolBar 的執行個體把它放在BNRDetailViewController's view 的底部。

 

A UIToolbar works a lot like a UINavigationBar – you can add instances of UIBarButtonItem to it.

where a navigation bar has two slots for bar button items, a toolbar has an array of bar button items. You can place as many bar button items in a toolbar as can fit on the screen.

一個navigation bar 有兩個位置存放bar button items ,a toolbar 有一列 bar button items. 你可以存放任意數量的bar button items 只有螢幕允許。

By default, a new instance of UIToolbar that is created in a XIB file comes with one UIBarButtonItem.

預設情況下,一個UIToolbar 已經有一個UIBarButtonItem。

Select this bar button item and open the attribute inspector. Change the Identifier to Camera, and the item will show a camera icon

選中這個bar button item 並開啟attribute inspector . 改變Identifier 到Camera,這樣就會顯示一個camera  標誌。

select the camera button by first clicking on the toolbar and then the button itself. Then Control-drag from the selected button to the implementation part of BNRDetailViewController.m

 

 

 

2 Taking Pictures and UIImagePickerController

In the takePicture: method, you will instantiate a UIImagePickerController and present it on the screen.

在takePic 中你將建立 一個UIImagePickerController並把它展現到螢幕上。

When creating an instance of UIImagePickerController, you must set its sourceType property and assign it a delegate.

當你建立一個UIImagePickerController的執行個體,你必須設定sourceType屬性並分配給他一個delegate。

2.1 Setting the image picker's sourceType

The sourceType constant that tells the image picker where to get images.

sourceType告訴image picker 從哪裡擷取images。

It has three possible values: 

(1)UIImagePickerControllerSourceTypeCamera

The user will take a new picture. 

使用者將獲得一個新的圖片

(2)UIImagePickerControllerSourceTypePhotoLibrary

The user will be prompted to select an album and then a photo from that album. 

將選擇一個album 和在這個albu的一個photo 。

UIImagePickerControllerSourceTypeSavedPhotosAlbum

The user picks from the most recently taken photos.

從最近使用的photos中擷取一個。 

 

The first source type, UIImagePickerControllerSourceTypeCamera, will not work on a device that does not have a camera. So, before using this type, you have to check for a camera by sending the message isSourceTypeAvailable: to the UIImagePickerController class:

第一個source type ,UIImagePickerControllerSourceTypeCamera不會在沒有相機的裝置中使用。所以,在使用該類型之前,你需要檢查一個camera 通過發送isSourceTypeAvailable給UIImagePickerController。

+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType;

 

- (IBAction)takePicture:(id)sender

{
UIImagePickerController *imagePicker =

[[UIImagePickerController alloc] init];

// If the device has a camera, take a picture, otherwise,

// just pick from photo library

if ([UIImagePickerController

isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

} else {

imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;

}

}

2.2 Setting the image picker's delegate

設定image picker 的delegate 

When the user selects an image from the UIImagePickerController's interface, the delegate is sent the message imagePickerController:didFinishPickingMediaWithInfo:. (If the user taps the cancel button, then the delegate receives the message imagePickerControllerDidCancel:.)

當使用者從UIImagePickerController's介面中選中一個image時,這個delegate (接收到訊息)被發送訊息給imagePickerController:didFinishPickingMediaWithInfo。(如果使用者選擇了cancel button那麼delegate會收到imagePickerControllerDidCancel訊息。)

The image picker's delegate will be the instance of BNRDetailViewController.

@interface BNRDetailViewController ()
<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

Why UINavigationControllerDelegate? 

為什麼會有UINavigationControllerDelegate ?

UIImagePickerController's delegate property is actually inherited from its superclass, UINavigationController, and while UIImagePickerController has its own delegate protocol, its inherited delegate property is declared to point to an object that conforms to UINavigationControllerDelegate.

UIImagePickerController's delegate屬性其實繼承自它的超類UINavigationController,當UIImagePickerController擁有它自己的delegate protocol ,它繼承的delegate 屬性被指向一個服從了UINavigationControllerDelegate的對象。

imagePicker.delegate = self;

 

2.3 Presenting the image picker modally

Once the UIImagePickerController has a source type and a delegate, it is time to get its view on the screen.

一旦UIImagePickerController有source type和delegate,就是該讓view 顯示在螢幕上了。

Unlike other UIViewController subclasses you have used, an instance of UIImagePickerController is presented modally.

不像其他的UIViewController的子類,UIImagePickerController的執行個體展現modally .

A modal view controller takes over the entire screen until it has finished its work.

一個modal view controller 佔用了整個螢幕直到它完成工作。

To present a view controller modally, you send presentViewController:animated:completion: to the UIViewController whose view is on the screen.

你需要發送presentViewController:animated:completion給誰的view 在螢幕上的UIViewController。 

// Place image picker on the screen
[self presentViewController:imagePicker animated:YES completion:nil];

2.4 Saving the image

you do not have a reference to the photo once the image picker is dismissed. To fix this, you are going to implement the delegate method imagePickerController:didFinishPickingMediaWithInfo:. This message is sent to the image picker's delegate when a photo has been selected.

你需要實現delegate 方法:imagePickerController:didFinishPickingMediaWithInfo。當一個photo 被選中,這個方法被送給image  picker的委託 。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{
// Get picked image from info dictionary
UIImage *image = info[UIImagePickerControllerOriginalImage];

// Put that image onto the screen in our image view

self.imageView.image = image;

// Take image picker off the screen -
// you must call this dismiss method
[self dismissViewControllerAnimated:YES completion:nil];

}

 

The solution, which you are going to implement in the next section, is to store images to disk and only fetch them into RAM when they are needed.

解決辦法是把圖片儲存到disk,當它們的時候再去建立他們。

 

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.