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,当它们的时候再去创建他们。

 

 

 

 

 

 

iOS Programming Camera 1的更多相关文章

  1. iOS Programming Camera 2

    iOS Programming Camera  2  1.1 Creating BNRImageStore The image store will fetch and cache the image ...

  2. iOS Programming Autorotation, Popover Controllers, and Modal View Controllers

    iOS Programming Autorotation, Popover Controllers, and Modal View Controllers  自动旋转,Popover 控制器,Moda ...

  3. iOS Programming State Restoration 状态存储

    iOS Programming State Restoration 状态存储 If iOS ever needs more memory and your application is in the ...

  4. Head First iOS Programming

    内部分享: Head First iOS Programming http://www.slideshare.net/tedzhaoxa/head-first-ios-programming-4606 ...

  5. iOS Programming Recipe 6: Creating a custom UIView using a Nib

    iOS Programming Recipe 6: Creating a custom UIView using a Nib JANUARY 7, 2013 BY MIKETT 12 COMMENTS ...

  6. iOS Programming Controlling Animations 动画

    iOS Programming Controlling Animations 动画 The word "animation" is derived from a Latin wor ...

  7. iOS Programming UIStoryboard 故事板

    iOS Programming UIStoryboard In this chapter, you will use a storyboard instead. Storyboards are a f ...

  8. iOS Programming NSUserDefaults

    iOS Programming NSUserDefaults  When you start an app for the first time, it uses its factory settin ...

  9. iOS Programming Localization 本地化

    iOS Programming Localization 本地化 Internationalization is making sure your native cultural informatio ...

随机推荐

  1. 《C++ Primer Plus》学习笔记3

    <C++ Primer Plus>学习笔记3 第8章 函数探幽 ============================================================== ...

  2. caioj1272&&codeforces 148D: 概率期望值3:抓老鼠

    这道真的是好题,不卡精度,不卡细节,但是思考的方式很巧妙! 一开始大家跟我想的应该差不多,用f[i][j]表示有i只白老鼠,j只黑老鼠的胜率,然后跑DP,然后我就发现,这样怎么做?还有一种不胜不负的平 ...

  3. 蓝牙4.0 BLE 广播包解析

    在使用EN-Dongle捕获和解析广播包之前,我们先了解一下BLE报文的结构,之后,再对捕获的广播包进行分析.在学习BLE的时候,下面两个文档是极其重要的,这是SIG发布的蓝牙的核心协议和核心协议增补 ...

  4. WIN8系统的远程桌面漏洞 利用QQ拼音纯净版实现提权

    前言 发现这个漏洞的时候, 笔者正在机房上课.正想用3389远程桌面去控制宿舍电脑的时候,因为重做系统忘记自己的IP地址,因此就随手扫描了一下IP段开3389端口的电脑. 没想到就随手扫描到一台WIN ...

  5. net share

    IT知识梳理 2017-11-30 06:57:10 Dos 命令进阶(一)讲解思路 1.Net常用命令 (1)net share - 查看共享命令 net share ipc$ - 设置ipc$共享 ...

  6. JS事件流与DOM事件处理程序

    在Javascript的DOM中,关于事件Event对象的知识是一定要掌握的.Event对象模型主要分为两个部分,一个是Event对象本身具有的属性和方法,这个参照API就可以学得:另一个是在DOM节 ...

  7. 小程序-demo:快速开始

    ylbtech-小程序-demo:快速开始 1.返回顶部 1.app.js //app.js App({ onLaunch: function () { // 展示本地存储能力 var logs = ...

  8. bzoj4276

    线段树优化建图+费用流 朴素的做法是每个强盗直接对每个区间的每个点连边,然后跑最大权匹配,这样有5000*5000条边,肯定过不去,那么我们用线段树优化一下,因为线段树能把一个O(n)的区间划分为O( ...

  9. bootstrap 表单元素、按钮、链接的禁用

    在Bootstra中,表单元素,按钮通过在标签内设置 disabled 或 disabled="disabled" 可以禁用表单元素,按钮.链接需要加入class="di ...

  10. 摘抄 - linux 目录结构简介

    /   根目录 |—–/bin   软连接,指向 /usr/bin.存储一些命令,一般为用户命令 |—-/boot  系统启动相关的文件;包括启动时内核的一些配置,grub配置等等:一般为之分配300 ...