ALAssetsLibrary类实现查看相册列表,增加相册,保存图片到相册等功能

该框架下有几个类,ALAssetsLibrary,ALAssetsGroup,ALAsset,ALAssetsFilter,ALAssetRepresentation。
ALAssetsLibrary类
ALAssetsLibrary类可以实现查看相册列表,增加相册,保存图片到相册等功能。


例如enumerateGroupsWithTypes方法列举所有相册。


ALAssetsGroup
ALAssetsGroup就是相册的类,可以通过valueForProperty方法查看不同属性的值,如:ALAssetsGroupPropertyName,相册名。


ALAssetsGroup类有几个方法,posterImage方法就是相册的封面图片,numberOfAssets方法获取该相册的图片视频数量,可以通过enumerateAssetsUsingBlock方法列举出所有照片。


ALAssetsGroup 可以使用setAssetsFilter:(ALAssetsFilter *)filter过滤照片或者视频等。


首先是获取所有相册,通过ALAssetsLibrary的实例方法得到ALAssetsGroup类数组。


    
ALAssetsLibrary *assetsLibrary;
    NSMutableArray *groupArray;
    assetsLibrary = [[ALAssetsLibrary alloc] init];
    groupArray=[[NSMutableArray alloc] initWithCapacity:1];
    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        if (group) {
            [groupArray addObject:group];
         
            //            通过这个可以知道相册的名字,从而也可以知道安装的部分应用
            //例如 Name:柚子相机, Type:Album, Assets count:1
            NSLog(@"%@",group);
        }
    } failureBlock:^(NSError *error) {
        NSLog(@"Group not found!\n");
    }];


ALAsset类
ALAsset类也可以通过valueForProperty方法查看不同属性的值,如:ALAssetPropertyType,asset的类型,有三种ALAssetTypePhoto, ALAssetTypeVideo or ALAssetTypeUnknown。


另外还可以通过该方法获取ALAssetPropertyLocation(照片位置),ALAssetPropertyDuration(视频时间),ALAssetPropertyDate(照片拍摄日期)等。


可以通过thumbnail方法就是获取该照片。


根据相册获取该相册下所有图片,通过ALAssetsGroup的实例方法得到ALAsset类数组。


 
[_group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
        if (result) {
            [imageArray addObject:result];
            NSLog(@"%@",result);
            iv.image=[UIImage imageWithCGImage: result.thumbnail];
            NSString *type=[result valueForProperty:ALAssetPropertyType];
        }
    }];


ALAssetRepresentation类


ALAsset类有一个defaultRepresentation方法,返回值是ALAssetRepresentation类,该类的作用就是获取该资源图片的详细资源信息。


//
//获取资源图片的详细资源信息
ALAssetRepresentation* representation = [asset defaultRepresentation];
//获取资源图片的长宽
CGSize dimension = [representation dimensions];
 //获取资源图片的高清图
[representation fullResolutionImage];
//获取资源图片的全屏图
[representation fullScreenImage];
//获取资源图片的名字
NSString* filename = [representation filename];
NSLog(@"filename:%@",filename);
//缩放倍数
[representation scale];
//图片资源容量大小
[representation size];
//图片资源原数据
 [representation metadata];
//旋转方向
[representation orientation];
 //资源图片url地址,该地址和ALAsset通过ALAssetPropertyAssetURL获取的url地址是一样的
NSURL* url = [representation url];
NSLog(@"url:%@",url);
//资源图片uti,唯一标示符
NSLog(@"uti:%@",[representation UTI]);

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{ 
 
    //呼出的菜单按钮点击后的响应
    if (buttonIndex == myActionSheet.cancelButtonIndex)
    {
        NSLog(@"取消");
    }
 
    switch (buttonIndex)
    {
        case 0:  //打开照相机拍照
            [self takePhoto];
            break; 
 
        case 1:  //打开本地相册
            [self LocalPhoto];
            break;
    }
}
 
//开始拍照
-(void)takePhoto
{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        //设置拍照后的图片可被编辑
        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        [picker release];
        [self presentModalViewController:picker animated:YES];
    }else
    {
        NSLog(@"模拟其中无法打开照相机,请在真机中使用");
    }
}
 
//打开本地相册
-(void)LocalPhoto
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
 
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    //设置选择后的图片可被编辑
    picker.allowsEditing = YES;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}
 
//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
 
{
 
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
 
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {
        //先把图片转成NSData
        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        NSData *data;
        if (UIImagePNGRepresentation(image) == nil)
        {
            data = UIImageJPEGRepresentation(image, 1.0);
        }
        else
        {
            data = UIImagePNGRepresentation(image);
        }
 
        //图片保存的路径
        //这里将图片放在沙盒的documents文件夹中
        NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];  
 
        //文件管理器
        NSFileManager *fileManager = [NSFileManager defaultManager];
 
        //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
        [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
 
        //得到选择后沙盒中图片的完整路径
        filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath,  @"/image.png"];
 
        //关闭相册界面
        [picker dismissModalViewControllerAnimated:YES];
 
        //创建一个选择后图片的小图标放在下方
        //类似微薄选择图后的效果
        UIImageView *smallimage = [[[UIImageView alloc] initWithFrame:
                                   CGRectMake(50, 120, 40, 40)] autorelease];    
 
        smallimage.image = image;
        //加在视图中
        [self.view addSubview:smallimage];
 
    } 
 
}
 
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    NSLog(@"您取消了选择图片");
    [picker dismissModalViewControllerAnimated:YES];
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值