iOS之Photos:访问某个相册通过collectionView显示

文中相关知识点较多,只记载重点思路,相关部分都有对应注释说明,部分还需要优化,只是工作学习的一种思路。

@import AVFoundation;

@import Photos;   导入框架

- (void)viewDidLoad {

[super viewDidLoad];

[self getallAblumInfo];得到相应相册的所有资源

}

- (void)getallAblumInfo{

_localDic = [NSMutableDictionary dictionary];表示对应日期的相应资源集合的键值对

_allKeys = [NSMutableArray array];所有键的集合(目的是字典不会排序,需要通过键值获取对应时间升序的value数组)

PHFetchOptions *options = [[PHFetchOptions alloc]init];

options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];通过创建日期升序遍历

PHFetchResult  *userCollections = [PHCollection fetchTopLevelUserCollectionsWithOptions:nil];获取用户创建的相册集合

for (PHCollection *c in userCollections) {

if ([c.localizedTitle isEqualToString:@"Test"]) { 条件:集合元数据的相册名为Test

PHAssetCollection *collection = (PHAssetCollection *)c;

PHFetchResult *results = [PHAsset fetchAssetsInAssetCollection:collection options:options];在test相册中通过options条件得到phasset资源集合

NSMutableArray *arr = [NSMutableArray array];

for (int i = 0; i < results.count; i++) {

按时间排序分组

PHAsset *asset1 = results[i];

NSString *str1 = [NSDateFormatter localizedStringFromDate:asset1.creationDate

dateStyle:NSDateFormatterMediumStyle

timeStyle:NSDateFormatterNoStyle];得到资源的年月日

if (i == 0) {

[_allKeys addObject:str1];

[arr addObject:asset1];

[_localDic setObject:arr forKey:str1];

}

if (i > 0) {

PHAsset *asset0 = results[i-1];

NSString *str0 = [NSDateFormatter localizedStringFromDate:asset0.creationDate

dateStyle:NSDateFormatterMediumStyle

timeStyle:NSDateFormatterNoStyle];

if ([str0 isEqualToString:str1]) {

[arr addObject:asset1];

[_localDic setObject:arr forKey:str1];添加元数据到字典

}else{

[_allKeys addObject:str1];

arr = [NSMutableArray array];

[arr addObject:asset1];

[_localDic setObject:arr forKey:str1];

}

}

}

}

}

}

条件2: uicollectionviewdatasource

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

LocalCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:LocalID forIndexPath:indexPath];

PHImageManager *manager = [PHImageManager defaultManager];

if (cell.tag) {

NSLog(@"tag = %ld",cell.tag);

[manager cancelImageRequest:(PHImageRequestID)cell.tag];

}

PHAsset *asset = [_localDic objectForKey:_allKeys[indexPath.section]][indexPath.row];

if (asset.mediaType == PHAssetMediaTypeImage) {资源为图片类型

PHImageRequestOptions *options = [[PHImageRequestOptions alloc]init];

options.resizeMode = PHImageRequestOptionsResizeModeFast;

options.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat;

options.synchronous = YES;

cell.tag = [manager requestImageForAsset:asset targetSize:CGSizeMake(300, 300) contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage *result, NSDictionary *info) {

cell.contentImg.image = result;获取缩略图

}];

}else if(asset.mediaType == PHAssetMediaTypeVideo){资源为视频类型

cell.tag = [manager requestAVAssetForVideo:asset options:nil resultHandler:^(AVAsset *asset1, AVAudioMix *audioMix, NSDictionary *info) {

AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc]initWithAsset:asset1];

generator.appliesPreferredTrackTransform = YES;将图片设置为正图

CMTime time = CMTimeMake(0.6, 30);当前时刻的图片

AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){

UIImage *thumbImg = [UIImage imageWithCGImage:image];

if (result == AVAssetImageGeneratorSucceeded) {

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

UIGraphicsBeginImageContextWithOptions(cell.contentImg.image.size, NO, 0.0);主线程中合并图片,主要是给缩略图加上一个播放按钮图区分

[thumbImg drawInRect:CGRectMake(0, 0, cell.contentImg.image.size.width, cell.contentImg.image.size.height)];

[[UIImage imageNamed:@"Play"] drawInRect:CGRectMake(cell.contentImg.image.size.width/2-cell.contentImg.image.size.width/6, cell.contentImg.image.size.height/2-cell.contentImg.image.size.height/6, cell.contentImg.image.size.width/3, cell.contentImg.image.size.height/3)];

UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

cell.contentImg.image = fullImage;

});

}

};

generator.maximumSize = CGSizeMake(200, 200);

[generator generateCGImagesAsynchronouslyForTimes:

[NSArray arrayWithObject:[NSValue valueWithCMTime:time]] completionHandler:handler];

}];

}

return cell;

}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

HeaderCollectionView *reusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];

reusable.lab.text = _allKeys[indexPath.section];设置头视图的日期

NSLog(@"text : %@",reusable.lab.text);

return reusable;

}

UICollectionViewLayoutDelegate     设置头视图的高度(垂直滚动)

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

return CGSizeMake(self.view.bounds.size.width, 44);

}

上一篇:前台界面(1)---HTML基本定义及常见标签


下一篇:hdoj 2601(判断N=i*j+i+j)