媒体层图形技术之AssetsLibrary 学习笔记

 

1.ALAsset

ALAsset类代表相册中的每个资源文件,可以通过它获取资源文件的相关信息还能修改和新建资源文件

 

Asset Properties

  • – valueForProperty:
  •   (1.ALAssetPropertyType 资源的类型(照片,视频)
  •    2.ALAssetPropertyLocation 资源地理位置(无位置信息返回null)
  •    3.ALAssetPropertyDuation 播放时长(照片返回ALErorInvalidProperty)
  •    4.ALAssetPropertyOrientation 方向(共有8个方向,参见:ALAssetOrientation)
  •    5.ALAssetPropertyDate 拍摄时间(包含了年与日时分秒)
  •    6.ALAssetPropertyRepresentations 描述(打印看了下,只有带后缀的名称)
  •    7.ALAssetPropertyURLs(返回一个字典,键值分别是文件名和文件的url)
  •    8.ALAssetPropertyAssetURL 文件的url )
  •   editable  property(指示资源是否可以编辑,只读属性)
  •   originalAsset  property(原始资源。若没有保存修改后资源,则原始资源为nil)

Accessing Representations

Setting New Image and Video Data

Saving to the Saved Photos Album

 保存video到Saved Photos album的指定路径

//通过ALAsset获取相对应的资源,获取图片的等比缩略图,原图的等比缩略

                        CGImageRef ratioThum = [asset aspectRatioThumbnail];

                        //获取相片的缩略图,该缩略图是相册中每张照片的poster图

                        CGImageRef thum = [asset thumbnail];

                        UIImage* rti = [UIImage imageWithCGImage:ratioThum];

                        UIImage* ti = [UIImage imageWithCGImage:thum];

                        UIImageView* v1 = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100, 120, 200)];

                        v1.image = rti;

                        v1.contentMode = UIViewContentModeScaleAspectFit;

                        UIImageView* v2 = [[UIImageView alloc]initWithFrame:CGRectMake(180, 100, 120, 200)];

                        v2.image = ti;

                        v2.contentMode = UIViewContentModeScaleAspectFit;

                        [self.view addSubview:v1];

                        [self.view addSubview:v2];

UIImage* ni = [UIImage imageNamed:@"new.png"];

                        //修改指定路径的图片资源内容,替换掉原来的内容

                        [asset setImageData:UIImageJPEGRepresentation(ni, 1.0) metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {

                            NSLog(@"new:%@",assetURL);

                        }];

//根据给定的图片内容,重新生成一张新图

                        [asset writeModifiedImageDataToSavedPhotosAlbum:UIImageJPEGRepresentation(ni, 1.0) metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {

                            NSLog(@"new:%@",assetURL);

                        }];

2.ALAssetRepresentation

ALAssetRepresentation类代表相册中每个资源文件的详细信息,可以通过它获取资源的大小,名字,路径等详细信息。

 

ALAssetRepresentation

 

---------------------------------------------------------------------------

        ALAssetRepresentation对象封装了一个给定ALAsset对象的陈述。

        一个在资源库中给定的asset可能有不止一个陈述。比如,如果一个相机提供RAW和JPEG格式的图像版本,

asset将有两个陈述版本,一个是RAW的,一个是JPEG的。

Getting Image Representations

Getting Image Attributes

Getting Raw Data

Getting Metadata

– UTI

– metadata

 //获取资源图片的详细资源信息

                        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]);

3.ALAssetsFilter

过滤器筛选出想要展现的资源:图片资源、视频资源

 

// Get all photos assets in the assets group.

+ (ALAssetsFilter *)allPhotos;

// Get all video assets in the assets group.

+ (ALAssetsFilter *)allVideos;

// Get all assets in the group.

+ (ALAssetsFilter *)allAssets;

4.ALAssetsGroup

 

Enumerating Assets(遍历资源)

- (void)setupAssets

{

    self.title = [self.assetsGroup valueForProperty:ALAssetsGroupPropertyName];

    

    if (!self.assets)

        self.assets = [[NSMutableArray alloc] init];

    else

        [self.assets removeAllObjects];

    

    ALAssetsGroupEnumerationResultsBlock resultsBlock = ^(ALAsset *asset, NSUInteger index, BOOL *stop) {



        if (asset)

        {

            [self.assets addObject:asset];

            

            NSString *type = [asset valueForProperty:ALAssetPropertyType];

            

            if ([type isEqual:ALAssetTypePhoto])

                self.numberOfPhotos ++;

            if ([type isEqual:ALAssetTypeVideo])

                self.numberOfVideos ++;

        }

        

        else if (self.assets.count > 0)

        {

            [self.collectionView reloadData];

            [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.assets.count-1 inSection:0]

                                        atScrollPosition:UICollectionViewScrollPositionTop

                                                animated:YES];

        }

    };

    

    [self.assetsGroup enumerateAssetsUsingBlock:resultsBlock];

}



Adding Assets(添加资源)

  • – addAsset:(添加一个已存在的asset到接收者。返回yes成功;反之,失败。)
  •   editable  property(指示程序是否可以编辑组,只读属性,打印看了一下,系统自带的是不能编辑的,其它可以编辑)

Filtering(过滤)

Accessing Properties(访问属性)

5.ALAssetsLibrary

代表系统中整个资源库,使用它可以访问资源库中的资源和保存照片,视频等功能。

 

 

 //判断当前应用是否能访问相册资源

    /*

     typedef NS_ENUM(NSInteger, ALAuthorizationStatus) {

     ALAuthorizationStatusNotDetermined = 0, 用户尚未做出了选择这个应用程序的问候

     ALAuthorizationStatusRestricted,        此应用程序没有被授权访问的照片数据。可能是家长控制权限。



     ALAuthorizationStatusDenied,            用户已经明确否认了这一照片数据的应用程序访问.

     ALAuthorizationStatusAuthorized         用户已授权应用访问照片数据.

     }

     */

    int author = [ALAssetsLibrary authorizationStatus];

    NSLog(@"author type:%d",author);

    //关闭监听共享照片流产生的频繁通知信息

    [ALAssetsLibrary disableSharedPhotoStreamsSupport];

    

    //创建一个相册到相册资源中,并通过block返回创建成功的相册ALAssetsGroup

    [_library addAssetsGroupAlbumWithName:@"test" resultBlock:^(ALAssetsGroup *group) {

        //NSString *const ALAssetsGroupPropertyName;

        //NSString *const ALAssetsGroupPropertyType;

        //NSString *const ALAssetsGroupPropertyPersistentID;

        //NSString *const ALAssetsGroupPropertyURL;

        //查看相册的名字

        NSLog(@"ALAssetsGroupPropertyName:%@",[group valueForProperty:ALAssetsGroupPropertyName]);

        //查看相册的类型

        NSLog(@"ALAssetsGroupPropertyType:%@",[group valueForProperty:ALAssetsGroupPropertyType]);

        //查看相册的存储id

        NSLog(@"ALAssetsGroupPropertyPersistentID:%@",[group valueForProperty:ALAssetsGroupPropertyPersistentID]);

        //查看相册存储的位置地址

        NSLog(@"ALAssetsGroupPropertyURL:%@",[group valueForProperty:ALAssetsGroupPropertyURL]);

        groupURL = [group valueForProperty:ALAssetsGroupPropertyURL];



    } failureBlock:^(NSError *error) {

        

    }];

新添加了一个名为test的相册

媒体层图形技术之AssetsLibrary 学习笔记


    //通过url地址在相册资源中获取该地址的资源文件ALAsset,有可能是相片或视频
    [_library assetForURL:[NSURL URLWithString:@""] resultBlock:^(ALAsset *asset) {
        /*
         NSString *const ALAssetPropertyType;
         NSString *const ALAssetPropertyLocation;
         NSString *const ALAssetPropertyDuration;
         NSString *const ALAssetPropertyOrientation;
         NSString *const ALAssetPropertyDate;
         NSString *const ALAssetPropertyRepresentations;
         NSString *const ALAssetPropertyURLs;
         NSString *const ALAssetPropertyAssetURL;
         */
        //查看资源的地理位置信息
        NSLog(@"ALAssetPropertyLocation:%@",[asset valueForProperty:ALAssetPropertyLocation]);
        //如果资源是视频,查看视频的时长
        NSLog(@"ALAssetPropertyDuration:%@",[asset valueForProperty:ALAssetPropertyDuration]);
        //查看资源的方向,图片的旋转方向
        NSLog(@"ALAssetPropertyOrientation:%@",[asset valueForProperty:ALAssetPropertyOrientation]);
        //查看资源的创建时间
        NSLog(@"ALAssetPropertyDate:%@",[asset valueForProperty:ALAssetPropertyDate]);
        //查看资源的描述信息
        NSLog(@"ALAssetPropertyRepresentations:%@",[asset valueForProperty:ALAssetPropertyRepresentations]);
        NSLog(@"ALAssetPropertyURLs:%@",[asset valueForProperty:ALAssetPropertyURLs]);
        //查看资源的url路径
        NSLog(@"ALAssetPropertyAssetURL:%@",[asset valueForProperty:ALAssetPropertyAssetURL]);
    } failureBlock:^(NSError *error) {
        
    }];
    //通过url地址获取相册资源中的一个相册
    [_library groupForURL:groupURL resultBlock:^(ALAssetsGroup *group) {
        NSLog(@"ALAssetsGroupPropertyName:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
    } failureBlock:^(NSError *error) {
    
    }];

//根据选择的类型遍历相册资源中的相对应类型的所有相册,其中stop行参是指针,表示是否停止迭代,当赋值为false则停止
    /*
     enum {
     ALAssetsGroupLibrary        = (1 << 0),
     ALAssetsGroupAlbum          = (1 << 1),
     ALAssetsGroupEvent          = (1 << 2),
     ALAssetsGroupFaces          = (1 << 3),
     ALAssetsGroupSavedPhotos    = (1 << 4),
     ALAssetsGroupPhotoStream    = (1 << 5),
     ALAssetsGroupAll            = 0xFFFFFFFF,
     };
     */
    [_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        NSLog(@"group name:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
    } failureBlock:^(NSError *error) {
        
    }];

利用资源库得枚举函数把资源组分类,代码如下:

 

- (void)setupGroup

{

    if (!self.assetsLibrary)

        self.assetsLibrary = [self.class defaultAssetsLibrary];

    

    if (!self.groups)

        self.groups = [[NSMutableArray alloc] init];

    else

        [self.groups removeAllObjects];

    

    CTAssetsPickerController *picker = (CTAssetsPickerController *)self.navigationController;

    ALAssetsFilter *assetsFilter = picker.assetsFilter;

    

    ALAssetsLibraryGroupsEnumerationResultsBlock resultsBlock = ^(ALAssetsGroup *group, BOOL *stop) {

        

        if (group)

        {

            [group setAssetsFilter:assetsFilter];

            

            if (group.numberOfAssets > 0)

                [self.groups addObject:group];

            

        }

        else

        {

            [self reloadData];

        }

    };

    

    

    ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error) {

        

        [self showNotAllowed];



    };

    

    // Enumerate Camera roll first

    [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos

                                      usingBlock:resultsBlock

                                    failureBlock:failureBlock];



    // Then all other groups

    NSUInteger type =

    ALAssetsGroupLibrary | ALAssetsGroupAlbum | ALAssetsGroupEvent |

    ALAssetsGroupFaces | ALAssetsGroupPhotoStream;

    

    [self.assetsLibrary enumerateGroupsWithTypes:type

                                      usingBlock:resultsBlock

                                    failureBlock:failureBlock];

}


其中得[self.class defaultAssetsLibrary]为单列形式:

 

#pragma mark - ALAssetsLibrary



+ (ALAssetsLibrary *)defaultAssetsLibrary

{

    static dispatch_once_t pred = 0;

    static ALAssetsLibrary *library = nil;

    dispatch_once(&pred, ^{

        library = [[ALAssetsLibrary alloc] init];

    });

    return library;

}



    //保存图片到系统默认的相册中,使用nsdata的形式,并返回照片的url地址
    [_library writeImageDataToSavedPhotosAlbum:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
        
    }];
    //保存图片到系统默认的相册中,使用cgimageref的形式,并返回照片的url地址
    [_library writeImageToSavedPhotosAlbum:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
        
    }];
    
    //保存图片到系统默认的相册中,使用cgimageref的形式,并且选择图片以什么旋转方向的形式保存,并返回照片的url地址
    /*
     typedef enum {
     ALAssetOrientationUp,            // default orientation
     ALAssetOrientationDown,          // 180 deg rotation
     ALAssetOrientationLeft,          // 90 deg CCW
     ALAssetOrientationRight,         // 90 deg CW
     ALAssetOrientationUpMirrored,    // as above but image mirrored along other axis. horizontal flip
     ALAssetOrientationDownMirrored,  // horizontal flip
     ALAssetOrientationLeftMirrored,  // vertical flip
     ALAssetOrientationRightMirrored, // vertical flip
     } ALAssetOrientation;
     */
    UIImage* image = [UIImage imageNamed:@"test.png"];
    [_library writeImageToSavedPhotosAlbum:[image CGImage] orientation:ALAssetOrientationLeft completionBlock:^(NSURL *assetURL, NSError *error) {
        NSLog(@"save image:%@",assetURL);
    }];

你可能感兴趣的:(library)