标签:
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]; }]; }
标签:
原文地址:http://blog.csdn.net/jwzhangjie/article/details/50880733