ALAssetsLibrary-代码操作iOS相册资源

简介: <p class="p1" style="color:rgb(54,46,43); font-family:Arial; font-size:14px; line-height:26px"> <span class="s1">在iOS中,我们调用摄像头和选择相册中的资源,我们可以使用:</span><span class="s2">UIImagePickerController</spa

在iOS中,我们调用摄像头和选择相册中的资源,我们可以使用:UIImagePickerController类来完成。


当然,我们也可以不使用UI的形式来访问iOS设备的相册资源。

那就是使用:ALAssetsLibrary


一、ALAssetsLibrary是什么


可以说,是一个桥梁把。连接了我们应用程序和相册之间的访问。

ALAssetsLibrary提供了我们对iOS设备中的相片、视频的访问。


ALAssetsLibrary被封装在 框架中。所以,我们在使用时,需要引入该框架。


贴:


self.view.backgroundColor = [UIColor whiteColor];

    self.assetsLibrary = [[ALAssetsLibrary allocinit];

    dispatch_queue_t dispatchQueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

    dispatch_async(dispatchQueue, ^(void) {

        // 遍历所有相册

        [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll

                                          usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

                                              // 遍历每个相册中的项ALAsset

                                              [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index,BOOL*stop) {

                                                  

                                                  __block BOOL foundThePhoto = NO;

                                                  if (foundThePhoto){

                                                      *stop = YES;

                                                  }

                                                  // ALAsset的类型

                                                  NSString *assetType = [result valueForProperty:ALAssetPropertyType];

                                                  if ([assetType isEqualToString:ALAssetTypePhoto]){

                                                      foundThePhoto = YES;

                                                      *stop = YES;

                                                      ALAssetRepresentation *assetRepresentation =[result defaultRepresentation];

                                                      CGFloat imageScale = [assetRepresentation scale];

                                                      UIImageOrientation imageOrientation = (UIImageOrientation)[assetRepresentation orientation];

                                                      dispatch_async(dispatch_get_main_queue(), ^(void) {

                                                          CGImageRef imageReference = [assetRepresentation fullResolutionImage];

                                                          // 对找到的图片进行操作

                                                          UIImage *image =[[UIImage allocinitWithCGImage:imageReferencescale:imageScale orientation:imageOrientation];

                                                          if (image != nil){

                                                              self.imageView = [[UIImageView allocinitWithFrame:self.view.bounds];

                                                              self.imageView.contentMode = UIViewContentModeScaleAspectFit;

                                                              self.imageView.image = image;

                                                              [self.view addSubview:self.imageView];

                                                          } else {

                                                              NSLog(@"Failed to create the image.");

                                                          } });

                                                  }

                                              }];

                                          }

                                          failureBlock:^(NSError *error) {

                                              NSLog(@"Failed to enumerate the asset groups.");

                                          }];

        

    });



乱啊,没办法,在xCode中是OK的。


需要解释的几点:


1.流程:该代码的流程,就是先遍历所有相册,然后,遍历每个相册中的第一张图片。


2.ALAssetsGroup:指代一个相册。


3.ALAsset每一个ALAsset代表一个单一资源文件(也就是一张图片,或者一个视频文件)


4.ALAssetRepresentationALAssetRepresentation封装了ALAsset,包含了一个资源文件中的很多属性。(可以说是ALAsset的不同的表示方式,本质上都表示同一个资源文件)


目录
相关文章
|
1月前
|
JSON JavaScript 安全
iOS应用程序数据保护:如何保护iOS应用程序中的图片、资源和敏感数据
iOS应用程序数据保护:如何保护iOS应用程序中的图片、资源和敏感数据
24 1
|
1月前
|
移动开发 安全 数据安全/隐私保护
iOS 全局自动化代码混淆工具!支持 cocoapod 组件代码一并混淆
iOS 全局自动化代码混淆工具!支持 cocoapod 组件代码一并混淆
|
3月前
|
移动开发 前端开发 安全
最强大的 iOS 应用源码保护工具:Ipa Guard,保护你的商业机密代码
最强大的 iOS 应用源码保护工具:Ipa Guard,保护你的商业机密代码
|
3月前
|
移动开发 前端开发 数据安全/隐私保护
【工具】iOS代码混淆工具-iOS源码混淆
【工具】iOS代码混淆工具-iOS源码混淆
42 1
|
2月前
|
移动开发 安全 数据安全/隐私保护
iOS 代码混淆和加固技术详解
iOS 代码混淆和加固技术详解
|
2月前
|
移动开发 前端开发 数据安全/隐私保护
iOS 代码混淆 - 从入门到放弃
iOS 代码混淆 - 从入门到放弃
|
2月前
|
安全 算法 数据安全/隐私保护
iOS 代码加固与保护方法详解 - 提升 iOS 应用安全性的关键步骤
iOS 代码加固与保护方法详解 - 提升 iOS 应用安全性的关键步骤
|
3月前
|
iOS开发
你知道IOS移动端到操作手势有哪些吗?
你知道IOS移动端到操作手势有哪些吗?
|
3月前
|
JSON JavaScript 安全
iOS 应用程序数据保护:如何保护 iOS 应用程序中的图片、资源和敏感数据
iOS 应用程序数据保护:如何保护 iOS 应用程序中的图片、资源和敏感数据
|
3月前
|
安全 Java Android开发
iOS代码安全加固利器:深入探讨字符串和代码混淆器的作用
iOS代码安全加固利器:深入探讨字符串和代码混淆器的作用
35 0