1.Privacy - Photo Library Usage Description 权限写入info.plist
2.自定义UICollectionViewController
#import <UIKit/UIKit.h>
#import <Photos/Photos.h>
#import "XMPickImageCollectionViewCell.h"
#import "XMPublishContentVideoController.h"
NS_ASSUME_NONNULL_BEGIN
@interface XMPickImageViewController : UICollectionViewController
@property (nonatomic,strong) NSMutableArray *photoArray;
@property (nonatomic,strong) NSMutableDictionary *imageDic;
@property (nonatomic,strong) NSMutableDictionary *vedioDic;
@end
NS_ASSUME_NONNULL_END
#import "XMPickImageViewController.h"
@interface XMPickImageViewController ()<UICollectionViewDelegate>
@end
@implementation XMPickImageViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.photoArray = [[NSMutableArray alloc] init];
PHFetchResult<PHAsset *> *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
self.imageDic = [[NSMutableDictionary alloc] init];
PHImageManager *imageManger = [PHImageManager defaultManager];
for (PHAsset *asset in fetchResult) {
[imageManger requestImageForAsset:asset targetSize:CGSizeMake(110, 110) contentMode:PHImageContentModeDefault options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
[self.photoArray addObject:result];
}];
}
fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:nil];
for (PHAsset *asset in fetchResult) {
[imageManger requestAVAssetForVideo:asset options:nil resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
if ([asset isKindOfClass:[AVURLAsset class]]) {
[self.photoArray addObject:asset];
}}];
}
[self.collectionView reloadData];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(110, 110);
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.photoArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
XMPickImageCollectionViewCell *cell =(XMPickImageCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"XMPickImageCollectionViewCell" forIndexPath:indexPath];
for(UIView *view in [cell.contentView subviews])
{
[view removeFromSuperview];
}
if ([self.photoArray[indexPath.row] isKindOfClass:[UIImage class]]) {
UIImageView *image = [[UIImageView alloc]initWithImage:self.photoArray[indexPath.row]];
image.clipsToBounds = true;
[cell.contentView addSubview:image];
}
if ([self.photoArray[indexPath.row] isKindOfClass:[AVURLAsset class]]) {
AVURLAsset* urlAsset = (AVURLAsset*)self.photoArray[indexPath.row];
NSString *key = [urlAsset.URL absoluteString];
AVPlayer *dVedio = [self.vedioDic objectForKey:key];
if (dVedio == nil) {
dVedio = [AVPlayer playerWithURL:urlAsset.URL];
}
[self.vedioDic setObject:dVedio forKey:key];
UIImage *dImage = [self.imageDic objectForKey:key];
if (dImage == nil) {
dImage = [XMPickImageViewController thumbnailImageForVideo:urlAsset.URL atTime:0.1];
}
[self.imageDic setObject:dImage forKey:key];
UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 110, 110)];
[image setImage:dImage];
image.clipsToBounds = true;
[cell.contentView addSubview:image];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 80, 13)];
NSLog(@"%@",urlAsset.URL);
CMTime time = [urlAsset duration];
long totalSeconds = ceil(time.value/10/60);
long branch = (floor)(totalSeconds/60.0);
long seconds = totalSeconds%60;
[label setText:[NSString stringWithFormat:@"%ld:%ld",branch,seconds]];
[label setTextAlignment:NSTextAlignmentRight];
[label setTextColor:[UIColor whiteColor]];
[cell.contentView addSubview:label];
}
return cell;
}
+ (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time {
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
NSParameterAssert(asset);
AVAssetImageGenerator *assetImageGenerator =[[AVAssetImageGenerator alloc] initWithAsset:asset];
assetImageGenerator.appliesPreferredTrackTransform = YES;
assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
CGImageRef thumbnailImageRef = NULL;
CFTimeInterval thumbnailImageTime = time;
NSError *thumbnailImageGenerationError = nil;
thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)actualTime:NULL error:&thumbnailImageGenerationError];
if(!thumbnailImageRef)
NSLog(@"thumbnailImageGenerationError %@",thumbnailImageGenerationError);
UIImage*thumbnailImage = thumbnailImageRef ? [[UIImage alloc]initWithCGImage: thumbnailImageRef] : nil;
return thumbnailImage;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
if ([self.photoArray[indexPath.row] isKindOfClass:[UIImage class]]) {
UIImage *image = self.photoArray[indexPath.row];
}
if ([self.photoArray[indexPath.row] isKindOfClass:[AVURLAsset class]]) {
AVURLAsset* urlAsset = (AVURLAsset*)self.photoArray[indexPath.row];
}
}
@end
行类
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface XMPickImageCollectionViewCell : UICollectionViewCell
@end
NS_ASSUME_NONNULL_END
#import "XMPickImageCollectionViewCell.h"
@implementation XMPickImageCollectionViewCell
@end