AssetsLibrary 实现访问相册,选取多张照片显示

  1 #import "ViewController.h"
  2 #import <AssetsLibrary/AssetsLibrary.h>
  3
  4
  5 /**
  6  *  ALAssetsLibrary.h 代表资源库(所有的视频,照片)
  7     ALAssetsGroup.h   代表资源库中的相册
  8     ALAsset.h         代表相册中一个视频或者一张照片
  9     ALAssetRepresentation.h 代表一个资源的描述,可以获取到原始图片
 10  */
 11
 12 @interface ViewController ()
 13
 14 @property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
 15
 16 @property (nonatomic,strong)NSMutableArray *array;
 17 @end
 18
 19 @implementation ViewController{
 20
 21     ALAssetsLibrary *library;
 22
 23 }
 24
 25 - (void)viewDidLoad {
 26     [super viewDidLoad];
 27
 28     //创建可变数组,存储资源文件
 29     _array = [NSMutableArray array];
 30
 31     //创建资源库,用于访问相册资源
 32     library = [[ALAssetsLibrary alloc] init];
 33
 34     //遍历资源库中所有的相册,有多少个相册,usingBlock会调用多少次
 35     [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
 36
 37         //如果存在相册,再遍历
 38         if (group) {
 39
 40             //遍历相册中所有的资源(照片,视频)
 41            [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
 42
 43                /*
 44                 资源的索引
 45                if (index == 2) {
 46
 47                 //停止遍历
 48                    *stop = YES;
 49                }
 50                 */
 51
 52                if (result) {
 53                    //将资源存储到数组中
 54                    [_array addObject:result];
 55                }
 56
 57            }];
 58         }
 59
 60         //刷新_collectionView reloadData;
 61          [_collectionView reloadData];
 62
 63     } failureBlock:^(NSError *error) {
 64
 65         NSLog(@"访问失败");
 66     }];
 67
 68 }
 69
 70 #pragma mark -UICollectionViewDelegate
 71
 72 //行的个数
 73 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
 74
 75
 76     return _array.count;
 77
 78 }
 79
 80 //创建UICollectionViewCell
 81 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
 82
 83     //如果单元格是在故事版中画出来的,不需要注册,需要在单元格中指定标识符
 84     UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
 85
 86     //取得图片视图
 87     UIImageView *imgView = (UIImageView *)[cell.contentView viewWithTag:1];
 88
 89     //取出对应的资源数据
 90      ALAsset *result =_array[indexPath.row];
 91
 92     //获取到缩略图
 93     CGImageRef cimg = [result thumbnail];
 94
 95     //转换为UIImage
 96     UIImage *img = [UIImage imageWithCGImage:cimg];
 97
 98     //显示图片
 99     imgView.image = img;
100
101     /**
102      *  获取到原始图片
103      ALAssetRepresentation *presentation = [result defaultRepresentation];
104
105     CGImageRef resolutionImg = [presentation fullResolutionImage];
106      */
107
108     return cell;
109
110 }
111
112 //单元格大小
113 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
114
115     return CGSizeMake(70, 70);
116
117 }
时间: 2024-08-26 01:09:34

AssetsLibrary 实现访问相册,选取多张照片显示的相关文章

iOS访问相册及拍照保存的实现实例

iOS访问相册及拍照保存的实现实例 实现效果: 1.点击访问相册按钮,可以访问系统相册,选择以后返回在imageView中显示 2.点击拍照,访问摄像头,实现以后保存在相册中,返回在imageView中显示 注意:拍照功能需要真机调试,模拟器无法实现 模拟器会有如下效果~弹出警告框 工程下载:github工程下载链接 下面是程序:注意此例中两个button和UIimageView在storyboard中添加: ViewController.h @interface ViewController

iOS开发中访问相册摄像像头

源码下载地址http://download.csdn.net/download/jingjingxujiayou/7270479 在AppDelegate.m文件中 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen

Android拍照、相册选取、裁剪图片

来自:http://blog.csdn.net/ryantang03/article/details/8656278 package com.example.listactivity; import java.io.ByteArrayOutputStream; import java.io.File; import com.example.model.ImageTools; import android.app.Activity; import android.app.AlertDialog;

iOS从手机相册选择一张照片并显示 Objective-C

要先给app设置访问相册的权限: 在项目的Info.plist文件里添加Privacy - Photo Library Usage Description权限 ViewController.h: 1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 { 5 IBOutlet UIImageView *myImageView; //与ImageView视图关联 6 } 7 8 9 - (IBAct

android4.0 的图库Gallery2代码分析(四) 之相册的数据处理以及显示

最近迫于生存压力,不得不给人兼职打工.故在博文中加了个求点击的链接.麻烦有时间的博友们帮我点击一下.没时间的不用勉强啊.不过请放心,我是做技术的,肯定链接没病毒,就是我打工的淘宝店铺.嘻嘻.http://shop108130013.taobao.com.谢谢捧场.以后就每周写篇原创的技术博客回报大家,实在是迫于生计,无所不用其极.请谅解. 相册的数据处理以及显示 相册的处理都包含在AlbumSetPage中.要明白相册的形成过程,一定要清楚AlbumSetPage的形成过程. AlbumSetP

C# 之 读取Word时发生 “拒绝访问” 及 “消息筛选器显示应用程序正在使用中” 异常的处理

1.Asp.net中建立Microsoft.Office.Interop.Word.Application时出现 “ 拒绝访问 ” 错误 项目中要实现在服务器端打开一个Word模版文件,修改其内容后再下载到客户端使用,在Asp.net页面中建立Microsoft.Office.Interop.Word.Application对象时出现“拒绝访问”的错误,提示信息如下: 拒绝访问. 说明: 执行当前 Web 请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的

iOS11访问相册权限变更问题

手机升到iOS 11后  发现之前正常的图片保存功能无法正常使用  会闪退 经测试发现应该是权限没有开启的原因  但是NSPhotoLibraryUsageDescription已经写入plist  且在iOS 11之前的手机系统上是正常的 通过查阅资料得知 OS11之后:默认开启访问相册权限(读权限),无需用户授权,无需添加NSPhotoLibraryUsageDescription,适配iOS11之前的还是需要加的. 添加图片到相册(写权限),需要用户授权,需要添加NSPhotoLibrar

python统计apache、nginx访问日志IP访问次数并且排序(显示前20条)

前言:python统计apache.nginx访问日志IP访问次数并且排序(显示前20条).其实用awk+sort等命令可以实现,用awk数组也可以实现,这里只是用python尝试下. apache脚本: ips = {} with open("/root/mail_access_log-20180629") as fh:     for line in fh:         ip = line.split(" ")[0]         if 6 < le

访问相册, 从相册中选取图片

@interface OneViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>{ UIImageView *imageView; } @end @implementation OneViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor yellow