// 拍照
if buttonIndex == 1 {
/*
.Camera 相机
.PhotoLibrary 图库
.Camera 相册
*/
let myBool = UIImagePickerController.isSourceTypeAvailable(.Camera);
if myBool == false {
print("不支持相机");
return;
}
// 打开相机
openTheImagePickerController(1);
return;
}
// 获取本地相册
if buttonIndex == 2 {
// 打开系统资源库
openTheImagePickerController(0);
return;
}
打开资源库,相机,相册
// MARK:
// MARK: 系统资源库,相机,相册
extension <#ViewController#> :UINavigationControllerDelegate,UIImagePickerControllerDelegate {
// MARK:
// MARK: 打开系统相册
/// 打开系统相册
private func openTheImagePickerController(sourceType: Int) {
// 打开相册
// ,UINavigationControllerDelegate,UIImagePickerControllerDelegate
let picker = UIImagePickerController();
picker.delegate = self;
// 是否 全屏观看
picker.allowsEditing = true;
// 资源库
if sourceType == 0 {
picker.sourceType = .PhotoLibrary;
}
// 相机
if sourceType == 1 {
picker.sourceType = .Camera;
}
// 相册
if sourceType == 2 {
picker.sourceType = .SavedPhotosAlbum;
}
// 跳转控制器
presentViewController(picker, animated: true, completion: nil);
}
// MARK:
// MARK: 选择 Choose 调用此方法
/// 选择 Choose 调用此方法
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
// 关闭控制器
picker.dismissViewControllerAnimated(true, completion: nil);
// 获取 图片
let image = info["UIImagePickerControllerOriginalImage"] as? UIImage ;
myImageView.image = image;
/*
此处info 有六个值
* UIImagePickerControllerMediaType; // an NSString UTTypeImage)
* UIImagePickerControllerOriginalImage; // a UIImage 原始图片
* UIImagePickerControllerEditedImage; // a UIImage 裁剪后图片
* UIImagePickerControllerCropRect; // an NSValue (CGRect)
* UIImagePickerControllerMediaURL; // an NSURL
* UIImagePickerControllerReferenceURL // an NSURL that references an asset in the AssetsLibrary framework
* UIImagePickerControllerMediaMetadata // an NSDictionary containing metadata from a captured photo
*/
}
}