iOS编程第四版第11章 Camera

本章讲述如何显示Camera拍摄的照片和图片库中的照片,如何保存这些照片。

涉及到的控件有UIImagePickerController和UIToolbar

model方面新建一个类用来存储图片。


实现步骤如下:

1. 添加一个UIImagePickerController 在DetailViewController中

2. 添加一个UIToolbar, 并在上面添加一个Camera button

3. 设置image picker的source type

三种类型:摄像头,图片库,最近打开

4. Camera按钮点击处理

- (IBAction)takePicture:(id)sender {
    
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }else{
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    
    imagePicker.delegate = self;
    
    //Place image picker on the screen
    [self presentViewController:imagePicker animated:YES completion:nil];
}

5. 显示图片

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

    UIImage *image = info[UIImagePickerControllerOriginalImage];
    
    //store the image in the ImageStore for this key
    [[ImageStore sharedStore] setImage:image forKey:self.item.itemKey];
    
    self.imageView.image = image;
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

6. 创建image model

 这里使用了一个可变字典来存储图片


7. 创建itemKey。用来唯一表示一个item。

这里用到了NSUUID这个类。生成一个UUID来做为itemKey。


8. 取消键盘显示。

1)对于textField可以使用代理UITextFieldDelegate

中的方法

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
<span style="white-space:pre">	</span>[textField resignFirstResponder];
<span style="white-space:pre">	</span>return YES;
}


2)点任意位置关闭键盘。

首先让UIViewController中的View为UIControl的一个实例。(属性中设置)

然后,绑定它的点击方法,

- (IBAction)backgroundTapped:(id)sender
{
<span style="white-space:pre">	</span>[self.view endEditing:YES];
}

9. 讲到为什么用#pragma mark







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值