How does IOS get local audio and video of a device?
1. Retrieve audio and video
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 stores all image information. PHAssetMediaTypeImage is the image search type.
typedef NS_ENUM(NSInteger, PHAssetMediaType) { PHAssetMediaTypeUnknown = 0, PHAssetMediaTypeImage = 1, PHAssetMediaTypeVideo = 2, PHAssetMediaTypeAudio = 3,} NS_ENUM_AVAILABLE_IOS(8_0);
2. obtain image 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 is a PHAsset in assetsFetchResults. targetSize is used to obtain the image size. This parameter is set based on the display method. If you need to obtain other image information, such as the title, you can use
[phAsset valueForKey:@"filename"];
3. Play a video
-(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]; }];}