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
#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