源码:https://github.com/potato512/SYSwiftLearning
UIImagePickerController主要用来选择图片,或是拍照,或是录像。其中图片选择时,既可以从相册选择,也可以从图库选择。
但不管是选择图片,还是拍照,还是录像都是通过设置属性"sourceType"来实现的。
// 注意添加相关framework头文件// 使用相机设备(AVCaptureDevice)功能时import Photos// 使用图片库功能import AssetsLibrary// 使用录制视频功能import MobileCoreServices
// MARK: 图库func photosShow(){ // 是否支持图片库 if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum) { if self.photosPicker == nil { // 实例化 self.photosPicker = UIImagePickerController() // 图库类型 self.photosPicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum // 代理对象(注意添加协议 UIImagePickerControllerDelegate、UINavigationControllerDelegate,及实现代理方法) self.photosPicker.delegate = self // 设置图片可编辑 self.photosPicker.allowsEditing = true // 改navigationbar背景色 self.photosPicker.navigationbar.barTintcolor = UIcolor.greencolor() // 改navigationbar标题色 self.photosPicker.navigationbar.TitleTextAttributes = [NSForegroundcolorAttributename: UIcolor.redcolor()] // 改navigationbar的button字体色 self.photosPicker.navigationbar.tintcolor = UIcolor.bluecolor() } // d出图库视图控制器 self.presentVIEwController(self.photosPicker,animated: true,completion: nil) } else { print("读取图库失败") }}
// MARK: 相册func imageShow(){ // 是否支持相册 if UIImagePickerController.isValIDImagePickerType(type: UIImagePickerType.UIImagePickerTypePhotolibrary) { if self.imagePicker == nil { self.imagePicker = UIImagePickerController() self.imagePicker.sourceType = UIImagePickerControllerSourceType.Photolibrary self.imagePicker.delegate = self self.imagePicker.setimagePickerStyle(UIcolor.browncolor(),Titlecolor: UIcolor.greencolor(),buttonTitlecolor: UIcolor.yellowcolor()) } // d出图库视图控制器 self.presentVIEwController(self.imagePicker,completion: nil) } else { print("读取相册失败") }}
// MARK: UIImagePickerControllerDelegate,UINavigationControllerDelegatefunc imagePickerController(picker: UIImagePickerController,dIDFinishPickingMediawithInfo info: [String : AnyObject]) { print("1 图片选择:\(info)") // 相册、图库 if picker.isEqual(self.imagePicker) || picker.isEqual(self.photosPicker) { // 选择的图片参数 /* 指定用户选择的媒体类型 UIImagePickerControllerMediaType 原始图片 UIImagePickerControllerOriginalimage 修改后的图片 UIImagePickerControllerEditedImage 裁剪尺寸 UIImagePickerControllerCropRect 媒体的URL UIImagePickerControllerMediaURL 原件的URL UIImagePickerControllerReferenceURL 当来数据来源是照相机的时候这个值才有效 UIImagePickerControllerMediaMetadata */ // 获取选择的原图 let pickedImage = info[UIImagePickerControllerOriginalimage] as! UIImage print(pickedImage) // 获取选择的图片引用路径(注意:添加类 import Photos) let pickedURL:NSURL = info[UIImagePickerControllerReferenceURL] as! NSURL let fetchResult:PHFetchResult = PHAsset.fetchAssetsWithALAssetURLs([pickedURL],options: nil) let asset:PHAsset = fetchResult.firstObject as! PHAsset PHImageManager.defaultManager().requestimageDataForAsset(asset,options: nil,resultHandler: { (imageData: NSData?,dataUTI: String?,orIEntation: UIImageOrIEntation,info: [NSObject : AnyObject]?) in //获取实际路径 let imageNSURL: NSURL = info!["PHImagefileURLKey"] as! NSURL print("路径:",imageNSURL) print("文件名:",imageNSURL.lastPathComponent) }) // 显示图片 self.imagevIEw.image = pickedImage // 图片保存到相册 // UIImageWritetoSavedPhotosAlbum(pickedImage,self,Selector("imageSave:error:contextInfo:"),nil) } // 退出图片选择控制器 picker.dismissVIEwControllerAnimated(true,completion: nil)} func imagePickerControllerDIDCancel(picker: UIImagePickerController) { print("2 放弃图片选择") // 退出图片选择控制器 picker.dismissVIEwControllerAnimated(true,completion: nil)}
// 扩展类方法import Foundationimport UIKitimport Photos/// 相片选择器类型:相册 Photolibrary,图库 SavedPhotosAlbum,相机 Camera,前置摄像头 Front,后置摄像头 Rearpublic enum UIImagePickerType:Int{ /// 相册 Photolibrary case UIImagePickerTypePhotolibrary = 1 /// 图库 SavedPhotosAlbum case UIImagePickerTypeSavedPhotosAlbum = 2 /// 相机 Camera case UIImagePickerTypeCamera = 3 /// 前置摄像头 Front case UIImagePickerTypeCameraFront = 4 /// 后置摄像头 Rear case UIImagePickerTypeCameraRear = 5}extension UIImagePickerController{ // MARK: - 设备使用有效性判断 // 相册 Photolibrary,图库 SavedPhotosAlbum,相机 Camera,前置摄像头 Front,后置摄像头 Rear public class func isValIDImagePickerType(type imagePickerType:UIImagePickerType) -> Bool { switch imagePickerType { case .UIImagePickerTypePhotolibrary: if self.isValIDPhotolibrary { return true } return false break case .UIImagePickerTypeSavedPhotosAlbum: if self.isValIDSavedPhotosAlbum { return true } return false break case .UIImagePickerTypeCamera: if self.isValIDCameraEnable && self.isValIDCamera { return true } return false break case .UIImagePickerTypeCameraFront: if self.isValIDCameraEnable && self.isValIDCameraFront { return true } return false break case .UIImagePickerTypeCameraRear: if self.isValIDCamera && self.isValIDCameraRear { return true } return false break default: return false } } /// 相机设备是否启用 public class var isValIDCameraEnable:Bool { get { let cameraStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeAudio) if cameraStatus == AVAuthorizationStatus.DenIEd { return false } return true } } /// 相机Camera是否可用(是否有摄像头) public class var isValIDCamera:Bool { get { if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) { return true } return false } } /// 前置相机是否可用 public class var isValIDCameraFront:Bool { get { if UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.Front) { return true } return false } } /// 后置相机是否可用 public class var isValIDCameraRear:Bool { get { if UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.Rear) { return true } return false } } /// 相册Photolibrary是否可用 public class var isValIDPhotolibrary:Bool { get { if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Photolibrary) { return true } return false } } /// 图库SavedPhotosAlbum是否可用 public class var isValIDSavedPhotosAlbum:Bool { get { if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum) { return true } return false } } // MARK: - 属性设置 func setimagePickerStyle(bgroundcolor:UIcolor,Titlecolor:UIcolor,buttonTitlecolor:UIcolor) { // 改navigationbar背景色 if let bgroundcolorTmp:UIcolor = bgroundcolor { self.navigationbar.barTintcolor = bgroundcolorTmp } // 改navigationbar标题色 if let TitlecolorTmp:UIcolor = Titlecolor { self.navigationbar.TitleTextAttributes = [NSForegroundcolorAttributename: TitlecolorTmp] } // 改navigationbar的button字体色 if let buttonTitlecolorTmp:UIcolor = buttonTitlecolor { self.navigationbar.tintcolor = buttonTitlecolorTmp } }}
需要注意:在使用过程中可能会出现UIImagePickerController的标题和按钮的字体为英文,可以通过修改配置信息实现中文显示。
修改方法:
1、添加中文语言包
2、修改手机或模拟器的语言
3、info.pList配置修改
总结
以上是内存溢出为你收集整理的swift中UIImagePickerController的使用(相册、图库)全部内容,希望文章能够帮你解决swift中UIImagePickerController的使用(相册、图库)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)