您的位置:首页 > 其它

打开系统相册,资源库,相机,选择图片

2016-05-23 00:00 363 查看


Swift/OC-->Demo点此下载

// 拍照
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
*/

}

}

判断是否支持资源库,相机,相册
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: