- <span style="font-size:18px;">
- #import "ViewController.h"
-
- @interface ViewController ()
-
- @property(nonatomic,strong)UITableView *tableView;
- @property(nonatomic,strong)NSMutableArray *assets;
- @property(nonatomic,strong)NSMutableArray *videoArr;
- @property(nonatomic,strong)ALAssetsLibrary *library;
-
- @end
-
- @implementation ViewController
-
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor blackColor];
-
- NSLog(@"%ld", [ALAssetsLibrary authorizationStatus]);
-
- self.assets = [[NSMutableArray alloc]initWithCapacity:0];
- self.videoArr = [[NSMutableArray alloc]initWithCapacity:0];
-
- self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, 320, 416) style:UITableViewStyleGrouped];
- self.tableView.delegate = self;
- self.tableView.dataSource = self;
- [self.view addSubview:self.tableView];
-
- UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, self.tableView.frame.size.height+70, 320, 80)];
- view.backgroundColor = [UIColor yellowColor];
- UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- btn1.frame = CGRectMake(10, 10, 100, 50);
- [btn1 setTitle:@"测试添加相册" forState:UIControlStateNormal];
- [btn1 addTarget:self action:@selector(addGroud) forControlEvents:UIControlEventTouchUpInside];
- [view addSubview:btn1];
- [self.view addSubview:view];
-
- self.library = [[ALAssetsLibrary alloc] init];
-
- void (^assetEnumerator)(ALAsset *, NSUInteger, BOOLBOOL *) = ^(ALAsset *result, NSUInteger index, BOOLBOOL *stop) {
- if(result != NULL) {
-
- if ([result valueForProperty:ALAssetPropertyType] == ALAssetTypeVideo) {
- [self.videoArr addObject:result];
- }else if([result valueForProperty:ALAssetPropertyType] == ALAssetTypePhoto){
- [self.assets addObject:result];
- }
-
- }
- };
-
-
- void (^assetGroupEnumerator)(ALAssetsGroup *, BOOLBOOL *) = ^(ALAssetsGroup *group, BOOLBOOL *stop) {
-
- if(group != nil) {
- [group enumerateAssetsUsingBlock:assetEnumerator];
- }
-
- [self.tableView reloadData];
- };
-
- [self.library enumerateGroupsWithTypes:ALAssetsGroupAll
- usingBlock:assetGroupEnumerator
- failureBlock: ^(NSError *error) {
- NSLog(@"便利所有相册");
-
- }];
-
-
- [self.tableView reloadData];
-
-
- }
-
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
- return 2;
- }
-
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
-
- if (section == 0) {
-
- if ([self.assets count]>5) {
- return 5;
- }else{
- return [self.assets count];
- }
-
- }else{
-
- if ([self.videoArr count]>5) {
- return 5;
- }else{
- return [self.videoArr count];
- }
-
- }
-
- }
-
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *CellIdentifier = @"Cell";
-
- UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
- }
-
-
- if (indexPath.section == 0) {
- ALAsset *asset = [self.assets objectAtIndex:indexPath.row];
-
- struct CGImage *imagecg = asset.thumbnail;
- UIImage *image = [UIImage imageWithCGImage:imagecg];
-
- [cell.imageView setImage:image];
- [cell.textLabel setText:[NSString stringWithFormat:@"Photo %d", indexPath.row+1]];
- }else if (indexPath.section == 1){
-
- ALAsset *asset = [self.videoArr objectAtIndex:indexPath.row];
-
- struct CGImage *imagecg = asset.thumbnail;
- UIImage *image = [UIImage imageWithCGImage:imagecg];
-
- [cell.imageView setImage:image];
- [cell.textLabel setText:[NSString stringWithFormat:@"video %d", indexPath.row+1]];
-
- }
-
- return cell;
- }
- </span>
添加一个图片到默认相册
- <span style="font-size:18px;">
- NSString *path = [[NSBundle mainBundle] pathForResource:@"123" ofType:@"png"];
- NSURL *urll = [NSURL fileURLWithPath:path];
- NSData *data = [NSData dataWithContentsOfURL:urll];
- [_library writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
-
- }];
-
-
- [_library writeImageDataToSavedPhotosAlbum:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
-
- }];</span>
创建一个相册
- <span style="font-size:18px;">- (void)addGroud{
- [self.library addAssetsGroupAlbumWithName:@"测试添加相册" resultBlock:^(ALAssetsGroup *group) {
- NSLog(@"addAssetsGroupAlbumWithName");
-
- NSLog(@"ALAssetsGroupPropertyName:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
-
- NSLog(@"ALAssetsGroupPropertyType:%@",[group valueForProperty:ALAssetsGroupPropertyType]);
-
- NSLog(@"ALAssetsGroupPropertyPersistentID:%@",[group valueForProperty:ALAssetsGroupPropertyPersistentID]);
-
- NSLog(@"ALAssetsGroupPropertyURL:%@",[group valueForProperty:ALAssetsGroupPropertyURL]);
- NSURL *groupURL = [group valueForProperty:ALAssetsGroupPropertyURL];
-
- } failureBlock:^(NSError *error) {
- NSLog(@"ERR : %@",error);
- }];
-
- }
- </span>
保存video到相册
- <span style="font-size:18px;">
-
- - (void)saveVideo:(NSString *)videoPath toAlbum:(NSString *)albumName withCompletionBlock:(SaveImageCompletion)completionBlock
- {
- [self writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:videoPath]
- completionBlock:^(NSURL *assetURL, NSError *error) {
- if (error!=nil) {
- completionBlock(error);
- return;
- }
-
-
- [self addAssetURL: assetURL
- toAlbum:albumName
- withCompletionBlock:completionBlock];
- }];
- }
-
-
- -(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock
- {
- __block BOOL albumWasFound = NO;
-
-
-
- [self enumerateGroupsWithTypes:ALAssetsGroupAlbum
- usingBlock:^(ALAssetsGroup *group, BOOLBOOL *stop) {
-
-
- if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {
-
-
- albumWasFound = YES;
-
-
-
- [self assetForURL: assetURL
- resultBlock:^(ALAsset *asset) {
-
-
- [group addAsset: asset];
-
-
- completionBlock(nil);
-
- } failureBlock: completionBlock];
-
-
- return;
- }
-
-
- if (group==nil && albumWasFound==NO) {
-
-
- ALAssetsLibrary* weakSelf = self;
-
-
- [self addAssetsGroupAlbumWithName:albumName
- resultBlock:^(ALAssetsGroup *group) {
-
-
- [weakSelf assetForURL: assetURL
- resultBlock:^(ALAsset *asset) {
-
-
- [group addAsset: asset];
-
-
- completionBlock(nil);
-
- } failureBlock: completionBlock];
-
- } failureBlock: completionBlock];
-
-
- return;
- }
-
- } failureBlock: completionBlock];
-
- }
- </span>
保存png到指定相册
- <span style="font-size:18px;">
-
- UIImage* image = [UIImage imageNamed:@"123"];
- [_library writeImageToSavedPhotosAlbum:[image CGImage] orientation:ALAssetOrientationLeft completionBlock:^(NSURL *assetURL, NSError *error) {
- NSLog(@"save image:%@",assetURL);
-
- [_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOLBOOL *stop) {
- NSString* groupname = [group valueForProperty:ALAssetsGroupPropertyName];
-
- if ([groupname isEqualToString:@"测试添加相册"]) {
-
- [group setAssetsFilter:[ALAssetsFilter allPhotos]];
-
- [_library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
-
- [group addAsset:asset];
-
- int count = [group numberOfAssets];
- NSLog(@"count:%d",count);
- dispatch_queue_t main = dispatch_get_main_queue();
- dispatch_async(main, ^{
-
- CGImageRef poster = [group posterImage];
-
- });
-
-
-
-
-
- NSLog(@"ALAssetsGroupPropertyName:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
-
- NSLog(@"ALAssetsGroupPropertyType:%@",[group valueForProperty:ALAssetsGroupPropertyType]);
-
- NSLog(@"ALAssetsGroupPropertyPersistentID:%@",[group valueForProperty:ALAssetsGroupPropertyPersistentID]);
-
- NSLog(@"ALAssetsGroupPropertyURL:%@",[group valueForProperty:ALAssetsGroupPropertyURL]);
-
-
- [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:0] options:NSEnumerationConcurrent usingBlock:^(ALAsset *result, NSUInteger index, BOOLBOOL *stop) {
-
- }];
-
- [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOLBOOL *stop) {
-
- }];
-
- [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOLBOOL *stop) {
-
- }];
-
- } failureBlock:^(NSError *error) {
-
- }];
- }
- } failureBlock:^(NSError *error) {
-
- }];
- }];
- </span>
参考文章:http://blog.csdn.net/kingsley_cxz/article/category/1466803
iOS中相册-用一个tableView区分照片和video
原文:http://www.cnblogs.com/allanliu/p/4207841.html