zoukankan      html  css  js  c++  java
  • iOS 照片多选

     我们知道UIKit框架为我们提供了UIImagePickerController 这个控制器来读取相册资源,但是通过这个控制器我们只能选取一张照片,但是如果想选取多张照片,又该如何操作呢?

    AssetsLibrary.framework 通过这个框架,我们可以读取到相册中所有的照片资源。这个框架主要提供了这么几个类:

    ALAssetsLibrary     指的是整个相册库
    ALAssetsGroup       指的是相册中的文件夹
    ALAsset             指的是文件夹中的照片、视频


    以上三个类的使用 :
    先通过  ALAssetsLibrary 类创建相册对象,再通过此相册对象循环遍历相册中得文件夹对象:ALAssetsGroup  。再通过每一个文件夹对象,循环遍历此文件夹中的所有的相册、视频对象:ALAsset。此对象中包含了相册、视频数据,通过这样几次遍历,我们就能获取到相册中所有的照片、视频数据。

    代码:

     1 ALAssetsLibrary *libray = [[ALAssetsLibrary alloc] init];  
    5 /* 6 7 通过相册库枚举遍历所有的文件夹ALAssetsGroup 8 9 usingBlock : 有多少个Group文件夹,则调用多少次block,每次将对应的文件夹Group传过来 10 11 */ 12 13 [libray enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 17 if (group != nil) { 21 //设置过滤对象 22 23 // ALAssetsFilter *filter = [ALAssetsFilter allVideos]; 24 25 // [group setAssetsFilter:filter]; 29 //通过文件夹枚举遍历所有的相片ALAsset对象,有多少照片,则调用多少次block 30 31 [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { 32 33 if (result != nil) { 34 35 //将result对象存储到数组中 36 37 [_data addObject:result]; 38 39 } 40 41 }]; 42 43 } 47 //刷新表格,显示照片 48 49 [_tableView reloadData]; 50 53 } failureBlock:^(NSError *error) { 54 55 56 57 }];

    以上代码通过循环遍历获取到所有的相册对象ALAsset,然后存储到数组对象_data中,再通过UITableView显示在单元格中。

     

    以下是单元格的创建,并且取得ALAsset对象中的数据,显示出来:

     

     1     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  
     2 
     3         return _data.count;  
     4 
     5     }  
    
     9     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
    10 
    13         static NSString *identify = @"imageCell";  
    
    17         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];  
    
    21         if (cell == nil) {  
    22 
    23             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify] autorelease];    
    24 
    25         }  
    
    29         ALAsset *asset = [_data objectAtIndex:indexPath.row];  
    32 
    33         //获取到媒体的类型  
    34 
    35         NSString *type = [asset valueForProperty:ALAssetPropertyType];  
    36 
    37         //媒体类型是视频  
    38 
    39         if ([type isEqualToString:ALAssetTypeVideo]) {  
    40 
    41             cell.textLabel.text = @"视频";  
    42 
    43         } else {  
    44 
    45             cell.textLabel.text = @"照片";  
    46 
    47         }  
    48   
    50 
    51         //获取到相片、视频的缩略图  
    52 
    53         CGImageRef cgImage = [asset thumbnail];  
    54 
    55         UIImage *image = [UIImage imageWithCGImage:cgImage];  
    56 
    
    59         cell.imageView.image = image;  
    60 
    61           
    65         return cell;  
    66 
    67     } 

     

    通过这样就能显示所有的相册中得图片

     

    参考博客:http://blog.csdn.net/kingsley_cxz/article/category/1466803

     
    更新:asset转data图片大小清晰度都不变
     1     [self.imageArray removeAllObjects];
     2     [MBProgressHUD showHUDAddedTo:self.view animated:YES];
     3     self.collectionView.hidden = YES;
     4     __weak typeof(self) weakSelf = self;
     5     
     6     dispatch_group_t group = dispatch_group_create();
     7     dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
     8         for (ALAsset *asset in weakSelf.assetsArray) {
     9             // 1.高清截图
    10 //            UIImage *image = [UIImage imageWithCGImage:asset.aspectRatioThumbnail];
    11 //            [weakSelf.imageArray addObject:image];//[UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]]
    12             // 2.asset转data图片大小清晰度都不变
    13             ALAssetRepresentation *image_representation = [asset defaultRepresentation];
    14             uint8_t *buffer = (Byte*)malloc(image_representation.size);
    15             NSUInteger length = [image_representation getBytes:buffer fromOffset: 0.0  length:image_representation.size error:nil];
    16             
    17             if (length != 0)  {
    18                 NSData *adata = [[NSData alloc] initWithBytesNoCopy:buffer length:image_representation.size freeWhenDone:YES];
    19                 UIImage *tempImg = [UIImage imageWithData:adata];
    20                 [weakSelf.imageArray addObject:tempImg];
    21             }
    22 
    23         }
    24     });
    25     dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    26         [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
    27         weakSelf.collectionView.hidden = NO;
    28         [weakSelf.collectionView reloadData];
    29     });
     
  • 相关阅读:
    武汉科技大学ACM :1004: 零起点学算法74——Palindromes _easy version
    武汉科技大学ACM :1003: 零起点学算法14——三位数反转
    武汉科技大学ACM :1002: 零起点学算法38——求阶乘和
    武汉科技大学ACM :1001: 零起点学算法34——继续求多项式
    cos实现文件上传--推荐
    文件夹分级保存文件
    apache_fileupload实现文件上传_上传多个文件
    UEFI模式下安装Win 7系统
    武汉科技大学ACM :1008: A+B for Input-Output Practice (VIII)
    武汉科技大学ACM :1007: A+B for Input-Output Practice (VII)
  • 原文地址:https://www.cnblogs.com/lxllanou/p/3974240.html
Copyright © 2011-2022 走看看