码迷,mamicode.com
首页 > 移动开发 > 详细

IOS 获取设备本地音视频

时间:2016-03-14 18:59:20      阅读:1101      评论:0      收藏:0      [点我收藏+]

标签:

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];
    }];
}








IOS 获取设备本地音视频

标签:

原文地址:http://blog.csdn.net/jwzhangjie/article/details/50880733

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!