- 效果图
- 数据源
UIImagePickerController
的数据源有以下三种类型:
UIImagePickerControllerSourceTypeCamera: 拍张新照片
UIImagePickerControllerSourceTypePhotoLibrary: 选择相册
UIImagePickerControllerSourceTypeSavedPhotosAlbum: 最近拍摄
可以根据是否支持相机 (isSourceTypeAvailable:
),选择不同数据源。示例代码:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// 若设备支持相机,使用拍照功能;否则从照片库中选择
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
imagePicker.delegate = self; //设置委托
- 委托
UIImagePickerController
对象的委托 (UIImagePickerControllerDelegate
) 通常应该设置为需要获取照片的对象。
当用户从 UIImagePickerController
对象选择一张照片后,委托会收到 imagePickerController:didFinishPickingMediaWithInfo:
消息(若取消,则会收到 imagePickerControllerDidCancel:
消息)
保存照片,示例代码:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
UIImage *image = info[UIImagePickerControllerOriginalImage]; //通过 info 字典获取选择的图片
self.imageView.image = image; //将照片放入 UIImageView 对象
// 关闭 UIImagePickerController 对象(即,选择图片之后返回)
[self dismissViewControllerAnimated:YES completion:nil];
}
- UUID
UUID: Universally Unique Identifier, 通用统一标识符,也称 GUID.
每个 NSUUID
类的对象都表示一个唯一的 UUID. UUID 是基于当前时间,计数器和硬件标识(通常为无线网卡的 Mac 地址)等数据计算生成的。字符串表示示例如下:
4A68S7H1-J2S9-2H29-2K78-IS1Y45Y3TC82
创建一个 NSUUID
对象,然后获取其 NSString
类型的值,示例代码:
NSUUID *uuid = [[NSUUID alloc] init];
NSString *key = [uuid UUIDString];
代码地址:
https://github.com/Ranch2014/iOSProgramming4ed/tree/master/11-Camera/Homepwner
《iOS编程(第4版)》 笔记
网友评论