1、检索音视频
PHFetchOptions *allPhotosOptions; @property (nonatomic, strong) PHFetchResult *assetsFetchResults; if (allPhotosOptions == nil) { allPhotosOptions = [[PHFetchOptions alloc] init]; allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]]; } self.assetsFetchResults = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosOptions];
当中assetsFetchResults存储了全部的图片信息。PHAssetMediaTypeImage为搜索图片的类型。相同的搜索其它类的能够依据
typedef NS_ENUM(NSInteger, PHAssetMediaType) {
PHAssetMediaTypeUnknown = 0,
PHAssetMediaTypeImage = 1,
PHAssetMediaTypeVideo = 2,
PHAssetMediaTypeAudio = 3,
} NS_ENUM_AVAILABLE_IOS(8_0);
2、获取图片UIImage[[PHCachingImageManager defaultManager] requestImageForAsset:asset
targetSize:AssetGridThumbnailSize
contentMode:PHImageContentModeAspectFill
options:nil
resultHandler:^(UIImage *result, NSDictionary *info) {
// Set the cell's thumbnail image if it's still showing the same asset.
if ([cell.representedAssetIdentifier isEqualToString:asset.localIdentifier]) {
cell.thumbnailImage = result;
}
}];
asset为assetsFetchResults中的一个PHAsset,targetSize是获取图片的大小,这个依据你显示的方式设置,假设须要获取图片其它信息比方标题能够採用[phAsset valueForKey:@"filename"];
3、播放视频
-(void)setVideoAsset:(PHAsset *)videoAsset { _videoAsset = videoAsset; [[PHImageManager defaultManager] requestPlayerItemForVideo:_videoAsset options:nil resultHandler:^(AVPlayerItem * _Nullable playerItem, NSDictionary * _Nullable info) { self.currentItem = playerItem; [self.player replaceCurrentItemWithPlayerItem:self.currentItem]; [self.currentItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil]; }]; }