camera

1.在.xib文件中拖一个UIToolbar对象到视图,该对象默认自带一个UIBarButtonItem对象。

2.使用UIImagePickerController
1.指定资源类型
2.设置代理对象
- (IBAction)takePicture:(id)sender {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    }else {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    }
//    NSArray *availableTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
//    [imagePicker setMediaTypes:availableTypes];
//    [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    
    //为什么self要实现UINavigationControllerDelegate,因为uiimagepickercontroller是uinavigationcontroller的子类
    [imagePicker setDelegate:self];
    
    //会retain imagePicker
    [self presentModalViewController:imagePicker animated:YES];
    
    [imagePicker release];
}

选中照片时,UIImagePickerController的委托对象会收到imagePickerController:didiFinishPickingMediaWithInfo:消息。
3.摄像
当设置好资源类型后,对象还需要知道要支持什么媒体类型,即当SourceType是UIImagePickerControllerSourceTypeCamera时,还要询问是摄像还是拍照或是都可以,这时从mediaTypes属性可得知。mediaTypes是一个数组,默认只有KUTTypeImage,这时候只允许拍照,要增加摄像就要在这个数组中添加常量字符串KUTTypeMovie。和检查是否有摄像头相似,availableMediaTypeForSourceType检测是否有摄像功能,并且返回一个包含支持媒体类型字符串的数组(即该设备拍照摄像都能支持则返回包含KUTTypeImage和KUTTypeImage常量的数组)。代码如下:
    NSArray *availableTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
    [imagePicker setMediaTypes:availableTypes];

如果想只限制拍照,保留默认值即可,如果想既能拍照又能拍视频则跟上面一样设置mediaTypes为消息返回值,那如果只想限制拍视频呢?没错,设置mediaTypes为只包含KUTTypeMovie的数组。
NSArray *availableTypes = [UIImagePickerController
availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
if ([availableTypes containsObject:(NSString *)kUTTypeMovie])
[ipc setMediaTypes:[NSArray arrayWithObject:(NSString *)kUTTypeMovie]];

KUTTypeMovie是CFStringRef类型,所以要转换为NSString 类型。并且要加入MobiaCoreServices框架和导入相应的头文件。

4.CFUUIDRef
在《ios编程》里将拍下来的照片用一个字典来存储,因为担心旧版本的内存不足而导致刚拍的照片被删除。所以就用一个单例的对象通过字典来存储照片,但这里要说的是如何生成唯一字符串,这里介绍Cocoa Touch提供的一种生成唯一标识(UUID)的机制,类型为CFUUIDRef的对象可以代表UUID,它是基于当前时间、计数器、和硬件标识等数据生成。
CFUUIDRef是由CoreFoundation API提供的C结构。Core Foundation “类”以CF为前缀,Ref为后缀。大多都有对应的OC版本,比如CFArrayRef对应NSArray。不多说,这些具体的东西上代码会比较好:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    NSString *oldKey = [_possession imageKey];
    if (oldKey) {
        [[ImageStore defaultImageStore] deleteImageForKey:oldKey];
    }
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    //创建一个CFUU对象,该对象知道如何创建无重复的字符串
    CFUUIDRef newUniqueID = CFUUIDCreate(kCFAllocatorDefault);
    //通过CFUU创建无重复的字符串
    CFStringRef newUniqueIDString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueID);
    [_possession setImageKey:(NSString *)newUniqueIDString];
    CFRelease(newUniqueIDString);
    CFRelease(newUniqueID);
    [[ImageStore defaultImageStore] setImage:image forkey:[_possession imageKey]];
    
    [imageView setImage:image];
    [self dismissModalViewControllerAnimated:YES];
    
}
5.隐藏键盘
方法一:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;

方法二:
把原来的UIView子类改为继承自UIControl(UIControl继承自UIView),在xib文件右键view拖到.h文件的方法区,设置好名字和Event。向self.view发送endEditing:消息,点击背景能隐藏键盘,因为这个消息会使textField之类的对象取消第一响应者。
- (IBAction)backgroundTapped:(id)sender
{
    [self.view endEditing:YES];
}


你可能感兴趣的:(ios学习)