首页 > IT职业 > IT图书 > 短视频软件开发:如何实现在iOS系统相册中选取视频
作为优质的短视频软件开发者,提高产品性能和增强用户体验是非常重要的,除此之外,为了使用户能够对产品保有一定的新鲜感,在短视频系统中加入了点击个人相册即可选取上传视频的功能。虽然看似是很基础且简单的功能,但是提现到短视频系统开发上就需要用源码来实现了。(以下源码仅供参考)
一、导入头文件
#import <Photos/Photos.h>1
二、设置监听相册变化代理并注册
PHPhotoLibraryChangeObserver [[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];12
三、实现代理方法
- (void)photoLibraryDidChange:(PHChange *)changeInstance {
dispatch_sync(dispatch_get_main_queue(), ^{
if ([PublicObj havePhotoLibraryAuthority]) {
[self getAlbumVideo];
}
[[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self];
});
}123456789
四、获取相册
PHFetchOptions *option = [[PHFetchOptions alloc] init]; option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld",PHAssetMediaTypeVideo]; option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];1234
//获取所有智能相册
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
PHFetchResult *streamAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumMyPhotoStream options:nil];
PHFetchResult *userAlbums = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
PHFetchResult *syncedAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumSyncedAlbum options:nil];
PHFetchResult *sharedAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumCloudShared options:nil];
NSArray *arrAllAlbums = @[smartAlbums, streamAlbums, userAlbums, syncedAlbums, sharedAlbums];
__block NSMutableArray *m_array = [NSMutableArray array];
for (PHFetchResult<PHAssetCollection *> *album in arrAllAlbums) {
[album enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL *stop) {1234567891011121314
//过滤PHCollectionList对象
if (![collection isKindOfClass:PHAssetCollection.class]) return;1
//过滤最近删除和已隐藏
if (collection.assetCollectionSubtype > 215 || collection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumAllHidden) return;12
//获取相册内asset result
PHFetchResult<PHAsset *> *result = [PHAsset fetchAssetsInAssetCollection:collection options:option];
NSString *title = collection.localizedTitle;
//NSLog(@"===6.26===%@====%@==%ld",result,title,(long)collection.assetCollectionSubtype);
if (!result.count) return;
if (collection.assetCollectionSubtype==PHAssetCollectionSubtypeSmartAlbumVideos) {
[result enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSMutableDictionary *m_dic = [NSMutableDictionary dictionary];
PHAsset *phAsset = (PHAsset *)obj;
//NSLog(@"===88=%@",phAsset);
NSString *position = [NSString string];
if ([PublicObj judgeAssetisInLocalAblum:phAsset]) {
position = @"location";
}else{
position = @"iCloud";
}
[m_dic setObject:position forKey:@"position"];
[self getVideoPathFromPHAsset:phAsset Complete:^(NSString *filePatch, NSString *dTime) {
if (filePatch && dTime) {
[m_dic setObject:filePatch forKey:@"filePath"];
[m_dic setObject:dTime forKey:@"time"];
}
}];
[self getVideoImageFromPHAsset:phAsset Complete:^(UIImage *image) {
if (!image) {
image = [UIImage imageNamed:@"公共-无数据"];
}
[m_dic setObject:image forKey:@"cover"];
}];
[m_array addObject:m_dic];
}];
}
}];
}
_albumArray = [NSArray arrayWithArray:m_array];1234567891011121314151617181920212223242526272829303132333435363738
以上,albumArray就是我们得到的相册中所有的视频,待短视频软件开发完成后即可在前端APP中体验到该功能。由此可见,在开发过程中不仅需要注意技术实现问题,还需要注意区别ios和Android系统。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2280/viewspace-2824432/ ,如需转载,请注明出处,否则将追究法律责任。