照片圖庫中的相片、視頻物件表現形式。
從資料庫獲取後所得到的檔案形式。
第一次調用 PHAsset fetchAssets 將會自動要求用戶授權, 或是主動調 PHPhotoLibrary.requestAuthorization: 要求用戶授權。
/// fetching assets
let result = PHPAsset.fetchAssets(with: .image, options: nil)
reuslt.enumerateObjects {
asset, index, stop in
// handle asset
}
/// PHPhotoLibrary request authorization
PHPhotoLibrary.requestAuthorization {
(status: PHAuthorizationStatus) in
if status == .authorized {
// fetching assets
}
}
Asset 只包含 metadata,需要調用 PHImageManager 下載,注意: Asset 所指向的數據有可能根本不在本地設備內。
PHImageManager.default().requestImage(for: asset, targetSize: imageView.size,
contentMode: .apaectFit, options: nil) {
(image, hashable) in
if let image = image {
DispatchQueue.main.async {
imageView.image = image
}
}
PHImageRequestOption 重要屬性
opportunistic
經過實測,”自動提供一個或多個結果“意思是會直接影響從 requestImage 取到的 UIImage。