[objc] view plaincopyprint?
- <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>
添加一个图片到默认相册
[objc] view plaincopyprint?
- <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) {
- }];
- //保存图片到系统默认的相册中,使用nsdata的形式,并返回照片的url地址
- [_library writeImageDataToSavedPhotosAlbum:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
- }];</span>
创建一个相册
[objc] view plaincopyprint?
- <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]);
- // //查看相册的存储id
- NSLog(@"ALAssetsGroupPropertyPersistentID:%@",[group valueForProperty:ALAssetsGroupPropertyPersistentID]);
- // //查看相册存储的位置地址
- NSLog(@"ALAssetsGroupPropertyURL:%@",[group valueForProperty:ALAssetsGroupPropertyURL]);
- NSURL *groupURL = [group valueForProperty:ALAssetsGroupPropertyURL];
- } failureBlock:^(NSError *error) {
- NSLog(@"ERR : %@",error);
- }];
- }
- </span>
保存video到相册
[objc] view plaincopyprint?
- <span style="font-size:18px;">
- //保存video到相册
- - (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;
- }
- //add the asset to the custom photo album
- [self addAssetURL: assetURL
- toAlbum:albumName
- withCompletionBlock:completionBlock];
- }];
- }
- -(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock
- {
- __block BOOL albumWasFound = NO;
- //search all photo albums in the library
- //遍历相册
- [self enumerateGroupsWithTypes:ALAssetsGroupAlbum
- usingBlock:^(ALAssetsGroup *group, BOOLBOOL *stop) {
- //compare the names of the albums
- if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {
- //target album is found
- albumWasFound = YES;
- //get a hold of the photo‘s asset instance
- //通过url得到asset
- [self assetForURL: assetURL
- resultBlock:^(ALAsset *asset) {
- //add photo to the target album
- [group addAsset: asset];
- //run the completion block
- completionBlock(nil);
- } failureBlock: completionBlock];
- //album was found, bail out of the method
- return;
- }
- //如果没有 创建相册加入asset
- if (group==nil && albumWasFound==NO) {
- //photo albums are over, target album does not exist, thus create it
- ALAssetsLibrary* weakSelf = self;
- //create new assets album
- [self addAssetsGroupAlbumWithName:albumName
- resultBlock:^(ALAssetsGroup *group) {
- //get the photo‘s instance
- [weakSelf assetForURL: assetURL
- resultBlock:^(ALAsset *asset) {
- //add photo to the newly created album
- [group addAsset: asset];
- //call the completion block
- completionBlock(nil);
- } failureBlock: completionBlock];
- } failureBlock: completionBlock];
- //should be the last iteration anyway, but just in case
- return;
- }
- } failureBlock: completionBlock];
- }
- </span>
保存png到指定相册
[objc] view plaincopyprint?
- <span style="font-size:18px;">
- //保存图片到系统默认的相册中,使用cgimageref的形式,并且选择图片以什么旋转方向的形式保存,并返回照片的url地址
- /*
- typedef enum {
- ALAssetOrientationUp, // default orientation
- ALAssetOrientationDown, // 180 deg rotation
- ALAssetOrientationLeft, // 90 deg CCW
- ALAssetOrientationRight, // 90 deg CW
- ALAssetOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip
- ALAssetOrientationDownMirrored, // horizontal flip
- ALAssetOrientationLeftMirrored, // vertical flip
- ALAssetOrientationRightMirrored, // vertical flip
- } ALAssetOrientation;
- */
- UIImage* image = [UIImage imageNamed:@"123"];
- [_library writeImageToSavedPhotosAlbum:[image CGImage] orientation:ALAssetOrientationLeft completionBlock:^(NSURL *assetURL, NSError *error) {
- NSLog(@"save image:%@",assetURL);
- //通过ALAssetsLibrary迭代取出所有相册
- [_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOLBOOL *stop) {
- NSString* groupname = [group valueForProperty:ALAssetsGroupPropertyName];
- //如果相册的名称是test的时候,对其进行操作
- if ([groupname isEqualToString:@"测试添加相册"]) {
- //设置相册组的筛选条件,ALAssetsFilter类表示筛选条件,allPhotos代表相册只包含相片,allVideos代表只包含视频,allAssets代表包含所有资源
- [group setAssetsFilter:[ALAssetsFilter allPhotos]];
- //通过刚保存的照片的url,把保存到默认相册的照片也保存到test相册中
- [_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];
- });
- //NSString *const ALAssetsGroupPropertyName;
- //NSString *const ALAssetsGroupPropertyType;
- //NSString *const ALAssetsGroupPropertyPersistentID;
- //NSString *const ALAssetsGroupPropertyURL;
- //查看相册的名字
- NSLog(@"ALAssetsGroupPropertyName:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
- //查看相册的类型
- NSLog(@"ALAssetsGroupPropertyType:%@",[group valueForProperty:ALAssetsGroupPropertyType]);
- //查看相册的存储id
- NSLog(@"ALAssetsGroupPropertyPersistentID:%@",[group valueForProperty:ALAssetsGroupPropertyPersistentID]);
- //查看相册存储的位置地址
- NSLog(@"ALAssetsGroupPropertyURL:%@",[group valueForProperty:ALAssetsGroupPropertyURL]);
- //按遍历顺序获取指定索引的资源,遍历顺序可以是先序或倒序
- /*
- enum {
- NSEnumerationConcurrent = (1UL << 0),
- NSEnumerationReverse = (1UL << 1),
- };
- typedef NSUInteger NSEnumerationOptions;
- */
- [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:0] options:NSEnumerationConcurrent usingBlock:^(ALAsset *result, NSUInteger index, BOOLBOOL *stop) {
- }];
- //按顺便遍历获取相册中所有的资源,index代表资源的索引,stop赋值为false时,会停止遍历
- [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOLBOOL *stop) {
- }];
- //按顺便遍历获取相册中所有的资源,遍历顺序可以是先序或倒序,index代表资源的索引,stop赋值为false时,会停止遍历
- [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
时间: 2024-11-07 14:49:42