本章讲述如何显示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