这个demo主要针对作业的第一个要求: showing all the multimedia files from Photo Album in a table view。需要用到 AssetsLibrary 来得到当前 Camera Roll 中所有的Asset,然后显示在 TableView 里面。
(注意:在这个demo里TableView 中显示的是所有的 video,而苏菲的要求是 all the multimedia files,也就是要包括 photos,实现方法见代码注释,友情建议:测试photo显示时,请先清一清自己之前的自拍,泪奔T_T)
另外,最好将 TableViewController 嵌在 Navigation Controller 里面使用(之前 第一版 里面没有这么做,现在补上了),因为后面加segue跳转到下一屏显示video或image的时候,必须要有 Navigation Controller才能实现跳转,只使用 TableViewController 是无法实现页面跳转的。
效果图:
步骤:
1. 新建一个工程,删掉 storyboard 中 default 的 ViewController,加入一个NavigationController,并设为 initial view controller。
2. 新建一个 UITableViewController 的 subclass,命名为 MyTableViewController。并将MyTableViewController 设为 Storyboard 中刚刚添加的 Root View Controller 的 Custom Class。
3. 不废话了,码代码。
//
// MyTableViewController.h
// AssetLibraryDemo
//
// Created by 鲁辰 on 7/5/15.
// Copyright (c) 2015 ChenLu. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <AVFoundation/AVFoundation.h>
@interface MyTableViewController : UITableViewController<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) ALAssetsLibrary *assetsLibrary;
@property (nonatomic, strong) NSMutableArray *videoTitleArray;
@property (nonatomic, strong) NSMutableArray *videoURLArray;
@property (nonatomic, strong) NSMutableArray *videoImageArray;
@end
//
// MyTableViewController.m
// AssetLibraryDemo
//
// Created by 鲁辰 on 7/5/15.
// Copyright (c) 2015 ChenLu. All rights reserved.
//
#import "MyTableViewController.h"
@implementation MyTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void) viewWillAppear:(BOOL)animated {
_videoTitleArray = [[NSMutableArray alloc] init];
_videoURLArray = [[NSMutableArray alloc] init];
_videoImageArray = [[NSMutableArray alloc] init];
[self buildAssetsLibrary];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table View Data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section{
return _videoTitleArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}
NSString *stringForCell;
stringForCell= [_videoTitleArray objectAtIndex:indexPath.row];
[cell.textLabel setText:stringForCell];
cell.imageView.image = [_videoImageArray objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - Show Video List Methods
- (void)buildAssetsLibrary
{
_assetsLibrary = [[ALAssetsLibrary alloc] init];
ALAssetsLibrary *notificationSender = nil;
NSString *minimumSystemVersion = @"4.1";
NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
if ([systemVersion compare:minimumSystemVersion options:NSNumericSearch] != NSOrderedAscending)
notificationSender = _assetsLibrary;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(assetsLibraryDidChange:) name:ALAssetsLibraryChangedNotification object:notificationSender];
[self updateAssetsLibrary];
}
- (void)assetsLibraryDidChange:(NSNotification*)changeNotification
{
[self updateAssetsLibrary];
}
- (void)updateAssetsLibrary
{
ALAssetsLibrary *assetLibrary = _assetsLibrary;
[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if (group)
{
[group setAssetsFilter:[ALAssetsFilter allVideos]];
//Please see: https://developer.apple.com/library/ios/documentation/AssetsLibrary/Reference/ALAssetsFilter_Class/
//To get all assets (photos and videos), use allAssets as filter.
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if (asset)
{
ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];
NSString *uti = [defaultRepresentation UTI];
NSURL *videoURL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];
NSString *title = [NSString stringWithFormat:@"%@", NSLocalizedString(@"Video", nil)];
[_videoTitleArray addObject:title];
[_videoURLArray addObject:videoURL];
CGImageRef thumbnailImageRef = [asset thumbnail];
UIImage *thumbnail = [UIImage imageWithCGImage:thumbnailImageRef];
[_videoImageArray addObject:thumbnail];
[self.tableView reloadData];
}
} ];
}
}
failureBlock:^(NSError *error)
{
NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];
}
@end
没有评论:
发表评论