由于只是簡(jiǎn)單的描述如何獲取系統(tǒng)相冊(cè)內(nèi)的照片和視頻,這里就不貼我那很丑的的布局的Demo了
步驟
1.plist文件添加訪問(wèn)權(quán)限
2.導(dǎo)入需要支持的庫(kù) import Photos
import Photos
3.獲取所有內(nèi)容,官方API里面有很多獲取到 PHFetchResult<PHAsset>,可自行獲取
lazy var photosArray : PHFetchResult<PHAsset> = {
let allOptions = PHFetchOptions()
// 按照時(shí)間排序
/// 這里設(shè)置的key = 'creationDate' 排序字段可以按照已有的屬性去查 ascending:升序和降序?qū)傩? /*
open class func fetchAssets(in assetCollection: PHAssetCollection, options: PHFetchOptions?) -> PHFetchResult<PHAsset>
open class func fetchAssets(withLocalIdentifiers identifiers: [String], options: PHFetchOptions?) -> PHFetchResult<PHAsset> // includes hidden assets by default
open class func fetchKeyAssets(in assetCollection: PHAssetCollection, options: PHFetchOptions?) -> PHFetchResult<PHAsset>?
open class func fetchAssets(withBurstIdentifier burstIdentifier: String, options: PHFetchOptions?) -> PHFetchResult<PHAsset>
// Fetches PHAssetSourceTypeUserLibrary assets by default (use includeAssetSourceTypes option to override)
open class func fetchAssets(with options: PHFetchOptions?) -> PHFetchResult<PHAsset>
open class func fetchAssets(with mediaType: PHAssetMediaType, options: PHFetchOptions?) -> PHFetchResult<PHAsset>
// assetURLs are URLs retrieved from ALAsset's ALAssetPropertyAssetURL
@available(iOS, introduced: 8.0, deprecated: 11.0, message: "Will be removed in a future release")
open class func fetchAssets(withALAssetURLs assetURLs: [URL], options: PHFetchOptions?) -> PHFetchResult<PHAsset>
*/
allOptions.sortDescriptors = [NSSortDescriptor.init(key: "creationDate", ascending: true)]
let allResults = PHAsset.fetchAssets(with: allOptions)
return allResults
}()
效果圖:這里使用的是兩個(gè)UICollectionView .

1565575061776.jpg
- 獲取某一個(gè)相冊(cè)內(nèi)的個(gè)體: let asset: PHAsset? = photosArray[indexPath.row]
5.以下貼官方API的關(guān)于PHAsset部分屬性的代碼
// Playback style describes how the asset should be presented to the user (regardless of the backing media for that asset). Use this value to choose the type of view and the appropriate APIs on the PHImageManager to display this asset
@available(iOS 11.0, *)
open var playbackStyle: PHAsset.PlaybackStyle { get }
/// 判斷獲取的是圖片,視頻,錄音,還是不知道...屬性
/*
public enum PHAssetMediaType : Int {
case unknown
case image
case video
case audio
}
*/
open var mediaType: PHAssetMediaType { get }
open var mediaSubtypes: PHAssetMediaSubtype { get }
open var pixelWidth: Int { get }
open var pixelHeight: Int { get }
open var creationDate: Date? { get }
open var modificationDate: Date? { get }
open var location: CLLocation? { get }
open var duration: TimeInterval { get }
// a hidden asset will be excluded from moment collections, but may still be included in other smart or regular album collections
open var isHidden: Bool { get }
open var isFavorite: Bool { get }
open var burstIdentifier: String? { get }
open var burstSelectionTypes: PHAssetBurstSelectionType { get }
open var representsBurst: Bool { get }
6.判斷asset.mediaType的屬性.
7.根據(jù)mediaType來(lái)根據(jù)類方法獲取想要的內(nèi)容,類方法有很多,自行根據(jù)需求獲取
如下:
if asset!.mediaType == .image{
let size = CGSize(width: asset!.pixelWidth, height: asset!.pixelHeight)
PHCachingImageManager.default().requestImage(for: asset!, targetSize: size, contentMode: .aspectFill, options: .none) { (img, dic) in
self.imageView.image = img
}
}
else if asset!.mediaType == .video {
let option = PHVideoRequestOptions()
option.version = .current
option.deliveryMode = .automatic
option.isNetworkAccessAllowed = true
PHCachingImageManager.default().requestAVAsset(forVideo: asset!, options: option) { (avsset, mix, dict) in
if avsset != nil {
// guard let uslAsset: AVURLAsset = avsset as? AVURLAsset else {return}
let generator = AVAssetImageGenerator.init(asset: avsset!)
generator.appliesPreferredTrackTransform = true
let time = CMTimeMakeWithSeconds(0.0,preferredTimescale: 600)
var actualTime:CMTime = CMTimeMake(value: 0,timescale: 0)
let imageRef:CGImage = try! generator.copyCGImage(at: time, actualTime: &actualTime)
DispatchQueue.main.async {
self.imageView.image = UIImage.init(cgImage: imageRef)
let getSeconds = NSInteger(round(CMTimeGetSeconds(avsset!.duration)))
let seconds = getSeconds%60
let minutes = getSeconds%3600/60
let hours = getSeconds/3600
}
}
}
}