博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Camera
阅读量:7239 次
发布时间:2019-06-29

本文共 5932 字,大约阅读时间需要 19 分钟。

Taking pictures and UIImagePickerController

Now you need a button to initiate the photo-taking process. It would be nice put this button on the navigation bar, but we will need the navigation bar for another button later. Instead, we will create an instance of UIToolbar and place it at the bottom of ItemDetailViewController's view.

A UIToolbar works lot like a UINavigationBar in that you can add UIBarButtonItems to it. However, where a navigation bar has two bar button items, a toolbar has an array of items. You can place as many UIBarButtonItems in a toolbar as can fit on the screen.

By default, a new instance of UIToolbar create in Interface Builder comes with one UIBarButtonItem. Select this bar button item and open the attribute inspector. Change the Identifier to Camera, and the item will show a camera icon.

- (void)takePicture:(id)sender{  UIImagePickerController *imagePicker =  [[UIImagePickerController alloc] init];  // If our device has a camera, we want to take a picture, otherwise, we  // just pick from photo library  if ([UIImagePickerController    isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {    [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];  } else {    [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];  }  // This line of code will generate 2 warnings right now, ignore them  [imagePicker setDelegate:self];  // Place image picker on the screen  [self presentModalViewController:imagePicker animated:YES];  // The image picker will be retained by ItemDetailViewController  // until it has been dismissed  [imagePicker release];}

When image's picked, delegate handler

- (void)imagePickerController:(UIImagePickerController *)pickerdidFinishPickingMediaWithInfo:(NSDictionary *)info{  // Get picked image from info dictionary  UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];  // Put that image onto the screen in our image view  [imageView setImage:image];  // Take image picker off the screen -  // you must call this dismiss method  [self dismissModalViewControllerAnimated:YES];}

To get around this problem, we must create a separate store for images. Instead of putting the image

directly into the imageView, we will put it into this store. Then when the ItemDetailViewController’s
view next appears on screen, we’ll have the ItemDetailViewController grab the image from the
image store and put it into its own imageView. In general, this is a best practice: a view controller
should re-populate its view’s subviews with data whenever it is sent the message viewWillAppear:,
eliminating the possibility that a low-memory warning could wipe out its content.

- (void)imagePickerController:(UIImagePickerController *)pickerdidFinishPickingMediaWithInfo:(NSDictionary *)info{  NSString *oldKey = [possession imageKey];  // Did the possession already have an image?  if (oldKey) {  // Delete the old image  [[ImageStore defaultImageStore] deleteImageForKey:oldKey];  }  UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];  // Create a CFUUID object - it knows how to create unique identifier strings  CFUUIDRef newUniqueID = CFUUIDCreate (kCFAllocatorDefault);  // Create a string from unique identifier  CFStringRef newUniqueIDString =  CFUUIDCreateString (kCFAllocatorDefault, newUniqueID);  // Use that unique ID to set our possessions imageKey  [possession setImageKey:(NSString *)newUniqueIDString];  // We used "Create" in the functions to make objects, we need to release them  CFRelease(newUniqueIDString);  CFRelease(newUniqueID);  // Store image in the ImageStore with this key  [[ImageStore defaultImageStore] setImage:image  forKey:[possession imageKey]];  // Put that image onto the screen in our image view  [imageView setImage:image];  // Take image picker off the screen  [self dismissModalViewControllerAnimated:YES];}

and in viewWillAppear:

- (void)viewWillAppear:(BOOL)animated{  [super viewWillAppear:animated];  [nameField setText:[possession possessionName]];  [serialNumberField setText:[possession serialNumber]];  [valueField setText:[NSString stringWithFormat:@"%d",  [possession valueInDollars]]];  NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init]    autorelease];  [dateFormatter setDateStyle:NSDateFormatterMediumStyle];  [dateFormatter setTimeStyle:NSDateFormatterNoStyle];  [dateLabel setText:  [dateFormatter stringFromDate:[possession dateCreated]]];  [[self navigationItem] setTitle:[possession possessionName]];  NSString *imageKey = [possession imageKey];  if (imageKey) {    // Get image for image key from image store    UIImage *imageToDisplay =    [[ImageStore defaultImageStore] imageForKey:imageKey];    // Use that image to put on the screen in imageView    [imageView setImage:imageToDisplay];  } else {    // Clear the imageView    [imageView setImage:nil];  }}

 Dismissing the keyboard

When the keyboard appears on the screen in the possession detail view, it obscures imageView. This is annoying.

to dismiss the keyboard, conform to UITextFieldDelegate protocol

@interface ItemDetailViewController : UIViewController
//In ItemDetailViewController.m, implement textFieldShouldReturn:.- (BOOL)textFieldShouldReturn:(UITextField *)textField{  [textField resignFirstResponder];  return YES;}

UIControl

We have seen how classes like UIButton can send an action message to a target when tapped. Buttons

inherit this target-action behavior from their superclass, UIControl. You’re going to change the view
of ItemDetailViewController from an instance of UIView to an instance of UIControl so that it can
handle touch events.

select the main view instance. Open the identity inspector and change the view’s class to UIControl.

choose

Connection: Action

Event: Touch Up Inside

- (IBAction)backgroundTapped:(id)sender{  [[self view] endEditing:YES];}

 

转载于:https://www.cnblogs.com/grep/archive/2012/06/16/2551988.html

你可能感兴趣的文章
职场上班族可吃零食能消除疲劳
查看>>
a.b.c.d.e.f.g这样的字段变成d.e.f.g的几种方法
查看>>
C++中关联容器和序列式容器在erase迭代器时的区别
查看>>
细谈围城---我的启示录
查看>>
字符串shuffle
查看>>
Nginx+PHP配置
查看>>
如何修改Xenserver网卡的offload
查看>>
Jmeter安装手记
查看>>
[视频教学]Maclean教你用Vbox在Enterprise Linux 5上安装Oracle 10gR2 RAC
查看>>
【Oracle Database 12c新特性】Online Statistics Gathering for Bulk-Load 针对批量数据加载的在线统计信息收集...
查看>>
windows下nginx 配置 tomcat 集群
查看>>
maven 常见故障处理
查看>>
Linux下安装mantis
查看>>
配置java环境变量时的一个陷阱(javapath)
查看>>
Python数据类型-类功能详解--【字符串】
查看>>
angular2 在header中带有继承的cookie
查看>>
maven规范:ssh框架整合
查看>>
KMP子串查找算法(二十六)
查看>>
硬盘SMART检测参数详解[转]
查看>>
异常解决java.io.IOException: invalid constant type: 15
查看>>