作为优质的短视频系统开发者,提高产品性能和增强用户体验是非常重要的,除此之外,为了使用户能够对产品保有一定的新鲜感,在短视频系统中加入了点击个人相册即可选取上传视频的功能。虽然看似是很基础且简单的功能,但是提现到短视频系统开发上就需要用源码来实现了。(以下源码仅供参考)
一、导入头文件
#import <Photos/Photos.h>
二、设置监听相册变化代理并注册
PHPhotoLibraryChangeObserver
[[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
三、实现代理方法
- (void)photoLibraryDidChange:(PHChange *)changeInstance {
dispatch_sync(dispatch_get_main_queue(), ^{
if ([PublicObj havePhotoLibraryAuthority]) {
[self getAlbumVideo];
}
[[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self];
});
}
四、获取相册
PHFetchOptions *option = [[PHFetchOptions alloc] init];
option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld",PHAssetMediaTypeVideo];
option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
//获取所有智能相册
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) {
//过滤PHCollectionList对象
if (![collection isKindOfClass:PHAssetCollection.class]) return;
//过滤最近删除和已隐藏
if (collection.assetCollectionSubtype > 215 ||
collection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumAllHidden) return;
//获取相册内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];
以上,albumArray就是我们得到的相册中所有的视频,待短视频系统开发完成后即可在前端APP中体验到该功能。由此可见,在开发过程中不仅需要注意技术实现问题,还需要注意区别ios和Android系统。想要了解其他的内容可直接关注作者,后续会继续分享相关内容文章。
声明:本文由作者原创,转载请注明原文出处、作者及原文链接。