取相册第一张图片--ALAssetsLibrary使用

来源:互联网 发布:淘宝登陆 编辑:程序博客网 时间:2024/04/28 01:24


转载: http://small.qiang.blog.163.com/blog/static/97849307201402133420155/


ALAssetsLibrary使用

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

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

一、ALAssetsLibrary是什么

可以说,是一个桥梁把。连接了我们应用程序和相册之间的访问。
ALAssetsLibrary提供了我们对iOS设备中的相片、视频的访问。

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

需添加AssetsLibrary.framework 
然后引入 

#import <AssetsLibrary/ALAsset.h>

#import <AssetsLibrary/ALAssetsLibrary.h>

#import <AssetsLibrary/ALAssetsGroup.h>

#import <AssetsLibrary/ALAssetRepresentation.h>


self.assetsLibrary= [[ALAssetsLibraryalloc]init];
    dispatch_queue_t dispatchQueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
    dispatch_async(dispatchQueue, ^(void) {
        // 遍历所有相册
        [self.assetsLibraryenumerateGroupsWithTypes:ALAssetsGroupAll
                                         usingBlock:^(ALAssetsGroup*group, BOOL*stop) {
                                              // 遍历每个相册中的项ALAsset
                                              [groupenumerateAssetsUsingBlock:^(ALAsset*result, NSUIntegerindex,BOOL*stop) {
                                                   
                                                  __blockBOOLfoundThePhoto = NO;
                                                  if(foundThePhoto){
                                                      *stop = YES;
                                                  }
                                                  // ALAsset的类型
                                                  NSString*assetType = [result valueForProperty:ALAssetPropertyType];
                                                  if([assetType isEqualToString:ALAssetTypePhoto]){
                                                      foundThePhoto = YES;
                                                      *stop = YES;
                                                      ALAssetRepresentation*assetRepresentation =[result defaultRepresentation];
                                                      CGFloatimageScale = [assetRepresentation scale];
                                                      UIImageOrientationimageOrientation = (UIImageOrientation)[assetRepresentation orientation];
                                                      dispatch_async(dispatch_get_main_queue(), ^(void) {
                                                          CGImageRefimageReference = [assetRepresentation fullResolutionImage];
                                                          // 对找到的图片进行操作
                                                          UIImage*image =[[UIImagealloc]initWithCGImage:imageReferencescale:imageScaleorientation:imageOrientation];
                                                          if(image != nil){
                                                              //获取到第一张图片
                                                          }else{
                                                              NSLog(@"Failed to create the image.");
                                                          } });
                                                  }
                                              }];
                                          }
                                       failureBlock:^(NSError*error) {
                                            NSLog(@"Failed to enumerate the asset groups.");
                                        }];
         
    });

0 0
原创粉丝点击