我使用"PHAsset"来获取相机胶卷资产.我在用
PHAsset.fetchAssetsWithMediaType(.Image, options: options)
用于获取所有图库图像.但我想同时获取所有图像和视频并在集合视图中显示(如Instagram相机视图).
请问任何人请告诉我该怎么做?
实际解决方案(swift 4,可能是swift 3):在你的viewDidLoad中,或者适用于你的案例调用checkAuthorizationForPhotoLibraryAndGet()
private func getPhotosAndVideos(){ let fetchOptions = PHFetchOptions() fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate",ascending: false)] fetchOptions.predicate = NSPredicate(format: "mediaType = %d || mediaType = %d", PHAssetMediaType.image.rawValue, PHAssetMediaType.video.rawValue) let imagesAndVideos = PHAsset.fetchAssets(with: fetchOptions) print(imagesAndVideos.count) } private func checkAuthorizationForPhotoLibraryAndGet(){ let status = PHPhotoLibrary.authorizationStatus() if (status == PHAuthorizationStatus.authorized) { // Access has been granted. getPhotosAndVideos() }else { PHPhotoLibrary.requestAuthorization({ (newStatus) in if (newStatus == PHAuthorizationStatus.authorized) { self.getPhotosAndVideos() }else { } }) } }
1)确保使用mediaType =%d而不是mediaType ==%d
2)确保您确实拥有授权并可以访问照片库,否则它将无声地失败.