照片选择器地址:
https://github.com/GaoQianpu/QPPhotoPickerDemo
照片选择器使用:
import UIKit
import Photos
/* 如果需要适配 iOS 10,请在info.plist中加入如下字段
* NSCameraUsageDescription --> 我们需要使用您的相机
* NSPhotoLibraryUsageDescription --> 我们需要访问您的相册
* 如不添加该字段,在iOS 10环境下会直接崩溃
*/
class ViewController: UIViewController {
//声明
var picker:QPPhotoPickerView?
overridefunc viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor =UIColor.cyan
QPPicker()
}
overridefunc didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//初始化并添加
/* 第一个参数,当前控制器
* 第二个参数,照片选择器的frame
*/
func QPPicker(){
picker =QPPhotoPickerView.init(controller:self, frame: CGRect.init(x:0, y: 150, width:UIScreen.main.bounds.width, height:200))
picker?.maxNum =9 //最大照片数量
self.view.addSubview(picker!)
}
//上传
func upLoadData(){
var dataArray = [Data]()
//数组中QPPhotoImageModel模型中:大图---小图---二进制Data
for modelin (picker?.QPPhotos)! {
dataArray.append(model.imageData!)
}
//上传Data数组
}
}
PHPhotoLibrary是Photos库最核心的类。它是我们的APP沟通用户照片库的桥梁。我们可以通过它增加、删除、修改用户的照片库。
// 获取用户授权
publicclass funcauthorizationStatus() -> PHAuthorizationStatus
// 通知用户授权,首次授权时可使用
publicclass funcrequestAuthorization(handler: (PHAuthorizationStatus) -> Void)
// 获取用户的照片库
publicclass funcsharedPhotoLibrary() -> PHPhotoLibrary
你可以在你的app中时刻监听照片库的状态
// 注册通知
public func registerChangeObserver(observer: PHPhotoLibraryChangeObserver)
// 注销通知
public funcunregisterChangeObserver(observer: PHPhotoLibraryChangeObserver)
用户的授权状态是一个枚举PHAuthorizationStatus。
publicenum PHAuthorizationStatus : Int {
case NotDetermined // 用户暂未权限认证
case Restricted // APP禁止使用相册权限认证
case Denied // 用户拒绝使用相册
case Authorized // 用户允许使用相册
}
由于整个用户都是关于照片的操作,故我们可以在AppDelegate.Swift授权。修改其func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
方法。
// Override point for customization after application launch.
print(__FUNCTION__)
switch PHPhotoLibrary.authorizationStatus() {
case PHAuthorizationStatus.NotDetermined: // 用户暂未权限认证
print("PHAuthorizationStatus.NotDetermined")
// 权限认证
PHPhotoLibrary.requestAuthorization { (status:PHAuthorizationStatus) -> Void in
print(status)
}
case PHAuthorizationStatus.Restricted: // APP禁止使用相册权限认证
print("PHAuthorizationStatus.Restricted")
case PHAuthorizationStatus.Denied: // 用户拒绝使用相册
print("PHAuthorizationStatus.Denied")
print("请进入 设置 -> 隐私 -> 相册 开启权限")
// 设置-隐私-相册
case PHAuthorizationStatus.Authorized: // 用户允许使用相册
print("PHAuthorizationStatus.Authorized")
}
returntrue
}
/// 从PHAssetCollection中获取PHAsset
////// - parameter assetCollection : PHAssetCollection
/// - parameter options : PHFetchOptions
////// - returns: PHFetchResult
publicclassfunc fetchAssetsInAssetCollection(assetCollection: PHAssetCollection, options: PHFetchOptions?) ->PHFetchResult
/// 通过localIdentifier获取PHAsset
////// - parameter identifiers : 对应PHAsset的localIdentifier属性
/// - parameter options : PHFetchOptions
////// - returns: PHFetchResult
publicclassfunc fetchAssetsWithLocalIdentifiers(identifiers: [String], options: PHFetchOptions?) ->PHFetchResult
/// 从PHAssetCollection中获取PHAsset
////// - parameter assetCollection : PHAssetCollection
/// - parameter options : PHFetchOptions
////// - returns: PHFetchResult
publicclassfunc fetchKeyAssetsInAssetCollection(assetCollection: PHAssetCollection, options: PHFetchOptions?) ->PHFetchResult?
/// 通过PHAsset的burstIdentifier属性获取PHAsset
////// - parameter burstIdentifier : 对应PHAsset的burstIdentifier属性
/// - parameter options : PHFetchOptions
////// - returns: PHFetchResult
publicclassfunc fetchAssetsWithBurstIdentifier(burstIdentifier: String, options: PHFetchOptions?) ->PHFetchResult
/// 从所有PHAsset中筛选PHAsset
////// - parameter options : PHFetchOptions
////// - returns: PHFetchResult
publicclassfunc fetchAssetsWithOptions(options: PHFetchOptions?) ->PHFetchResult
/// 通过类型获取PHAsset
////// - parameter mediaType : PHAssetMediaType
/// - parameter options : PHFetchOptions
////// - returns: PHFetchResult
publicclassfunc fetchAssetsWithMediaType(mediaType: PHAssetMediaType, options: PHFetchOptions?) ->PHFetchResult
/// 通过ALAsset的ALAssetPropertyAssetURL地址获取PHAsset
////// - parameter assetURLs : [NSURL]
/// - parameter options : PHFetchOptions
////// - returns: PHFetchResult
publicclassfunc fetchAssetsWithALAssetURLs(assetURLs: [NSURL], options: PHFetchOptions?) ->PHFetchResult
publicvar mediaType: PHAssetMediaType { get }
/// 资产类型细分
publicvar mediaSubtypes: PHAssetMediaSubtype { get }
/// 资产来源,如icloud、用户创建
@available(iOS9.0, *)
publicvar sourceType: PHAssetSourceType { get }
/// 像素宽
publicvar pixelWidth: Int { get }
/// 像素高
publicvar pixelHeight: Int { get }
/// 创建时间
publicvar creationDate: NSDate? { get }
/// 修改时间
publicvar modificationDate: NSDate? { get }
/// 地址
publicvar location: CLLocation? { get }
/// 视频播放时间(秒)publicvar duration: NSTimeInterval { get }
/// 是否隐藏publicvar hidden: Bool { get }
/// 是否收藏publicvar favorite: Bool { get }