IOS进阶之AssetsLibrary.framework

AssetsLibrary.framework

官方文档

是用来访问手机用户的媒体数据的框架,这里的媒体数据是指手机照片应用的图片,视频,livephto等

只支持到IOS9.0 ,9.0以后官方推荐使用Photo.framework


主要的类

ALAsset

照片应用的图片,视频,livephto等实例

ALAssetsFilter

提供了对一组ALAsset的滤镜行为

ALAssetsGroup

一组ALAsset,与照片应用的顺序是一一对应的

ALAssetsLibrary

提供了访问照片应用里图片,video,或者livephoto的方法

ALAssetRepresentation

ALAsset的表示和说明,包括UTI,尺寸规格,文件大小,方向,缩放比例,文件名等等

<span style="font-size:14px;">self.assetsLibrary = [[ALAssetsLibrary alloc] init];
    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 alloc] initWithCGImage:imageReference scale:imageScale orientation:imageOrientation];
                                                          if (image != nil){
                                                              //获取到第一张图片
                                                          } else {
                                                              NSLog(@"Failed to create the image.");
                                                          } });
                                                  }
                                              }];
                                          }
                                        failureBlock:^(NSError *error) {
                                            NSLog(@"Failed to enumerate the asset groups.");
                                        }];
         
    });</span>

下面利用第三方框架

写一个选择多张图片的demo

ZYQAssetPickerController下载

#import "MainViewController.h"
#import "ZYQAssetPickerController.h"
#import "AppDelegate.h"
@interface MainViewController ()<ZYQAssetPickerControllerDelegate,UINavigationControllerDelegate,UIScrollViewDelegate>{
    UIButton *btn;
    
    UIScrollView *src;
    
    UIPageControl *pageControl;
}

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    btn=[[UIButton alloc] init];
    btn.frame=CGRectMake(60., self.view.frame.size.height-80, self.view.frame.size.width-120, 60);
    [btn setTitle:@"Open" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    src=[[UIScrollView alloc] initWithFrame:CGRectMake(20, 20, self.view.frame.size.width-40, self.view.frame.size.height-120)];
    src.pagingEnabled=YES;
    src.backgroundColor=[UIColor lightGrayColor];
    src.delegate=self;
    [self.view addSubview:src];
    
    pageControl=[[UIPageControl alloc] initWithFrame:CGRectMake(src.frame.origin.x, src.frame.origin.y+src.frame.size.height-20, src.frame.size.width, 20)];
    [self.view addSubview:pageControl];
    

}

-(void)btnClick:(id)sender{

 
    ZYQAssetPickerController *picker = [[ZYQAssetPickerController alloc] init];    
    picker.maximumNumberOfSelection = 9;
    picker.assetsFilter = [ALAssetsFilter allPhotos];
    picker.showEmptyGroups=NO;
    picker.delegate=self;
    picker.selectionFilter = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        if ([[(ALAsset*)evaluatedObject valueForProperty:ALAssetPropertyType] isEqual:ALAssetTypeVideo]) {
            NSTimeInterval duration = [[(ALAsset*)evaluatedObject valueForProperty:ALAssetPropertyDuration] doubleValue];
            return duration >= 5;
        } else {
            return YES;
        }
    }];
    
    [self presentViewController:picker animated:YES completion:NULL];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - ZYQAssetPickerController Delegate
-(void)assetPickerController:(ZYQAssetPickerController *)picker didFinishPickingAssets:(NSArray *)assets{
    [src.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        src.contentSize=CGSizeMake(assets.count*src.frame.size.width, src.frame.size.height);
        dispatch_async(dispatch_get_main_queue(), ^{
            pageControl.numberOfPages=assets.count;
        });

        for (int i=0; i<assets.count; i++) {
            ALAsset *asset=assets[i];
            UIImageView *imgview=[[UIImageView alloc] initWithFrame:CGRectMake(i*src.frame.size.width, 0, src.frame.size.width, src.frame.size.height)];
            imgview.contentMode=UIViewContentModeScaleAspectFill;
            imgview.clipsToBounds=YES;
            UIImage *tempImg=[UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage];
            dispatch_async(dispatch_get_main_queue(), ^{
                [imgview setImage:tempImg];
                [src addSubview:imgview];
            });
        }
    });
}

#pragma mark - UIScrollView Delegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    pageControl.currentPage=floor(scrollView.contentOffset.x/scrollView.frame.size.width);;
}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值