iOS 6编程-使用Photo Library(照片库)和相机

将照片库和App集成,可直接访问存储在iPhone、iPad 中的任何图像或拍摄新照片,并在App 中使用。

1. 为了使用 UIImagePickerController,需要将类声明为遵守2个协议:UIImagePickerControllerDelegate和UINavigationControllerDelegate。

@interface NoteViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

2. 显示图像选择器

下面是点击设置按钮后,需要调用的方法 – 显示图像选择器,让用户选择图像。
- (IBAction)settingButton:(id)sender {
UIImagePickerController *imagePicker;
imagePicker = [[UIImagePickerController alloc] init];

imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:nil];
// [self presentModalViewController:imagePicker animated:YES];
}

上述注释的代码,在iOS 6  中不赞成使用。

3. 显示选定的图像

在用户选择好图像之后,为了对用户选择的图像做出响应,还需要实现委托方法imagePickerController:didFinishPickingMediaWithInfo:,下面是具体的代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[self dismissViewControllerAnimated:YES completion:nil];
//[self dismissModalViewControllerAnimated:YES];
self.backgroundImageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
}

实现在当前场景的UIImageView元素中显示用户选择的图像。上述注释的代码,在iOS 6  中不赞成使用。





posted @ 2012-10-17 16:34  周文  阅读(383)  评论(0编辑  收藏  举报