【iOS开发】---- ALAsset,ALAssetsLibrary,ALAssetsgroup常见属性及用法

ALAssetsgroup

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

Enumerating Assets(遍历资源)

Adding Assets(添加资源)

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

Filtering(过滤)

Accessing Properties(访问属性)



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的指定路径


ALAssetRepresentation

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

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

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

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

Getting Image Representations

Getting Image Attributes

Getting Raw Data

Getting Metadata

Getting an URL


使用

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


       利用ALAssetsLibrary来获取相册的分组:


-(void)getGroup
{
    @autoreleasepool
    {
        ALAssetsLibraryAccessFailureBlock failureblock =
        ^(NSError *myerror)
        {
            NSLog(@"相册访问失败 =%@", [myerror localizedDescription]);
            if ([myerror.localizedDescription rangeOfString:@"Global denied access"].location!=NSNotFound) {
                NSLog(@"无法访问相册.请在'设置->定位服务'设置为打开状态.");
            }else{
                NSLog(@"相册访问失败.");
            }
        };
        
        ALAssetsLibraryGroupsEnumerationResultsBlock
        libraryGroupsEnumeration = ^(ALAssetsGroup* group,BOOL* stop)
        {
            if (group!=nil)
            {
                [self.groupArray addObject:group];
            }
            else
            {
                if (!_groupTable)
                {
                    _groupTable = [[UITableView alloc] initWithFrame:EZRECT(0, 0, SCREEN_SIZE_WIDTH, SCREEN_SIZE_HEIGHT - 44)
                                                               style:UITableViewStylePlain];
                    _groupTable.delegate = self;
                    _groupTable.dataSource = self;
                    [self.view addSubview:_groupTable];
                }
                [_groupTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
            }
        };
        [[DataCenter defaultAssetsLibrary] enumerateGroupsWithTypes:ALAssetsGroupAll
                                                         usingBlock:libraryGroupsEnumeration
                                                       failureBlock:failureblock];
    }
}


       由于分组在添加到groupArray中,后面要在外面的函数中调用,这样会发生警告,所以,将ALAssetsLibrary的获取写成一个单例

+ (ALAssetsLibrary *)defaultAssetsLibrary
{
    static dispatch_once_t pred = 0;
    static ALAssetsLibrary *library = nil;
    dispatch_once(&pred,
                  ^{
                      library = [[ALAssetsLibrary alloc] init];
                  });
    return library;
}


     利用获得的分组再来获取资源文件:

-(void)filterImageWithGroup:(ALAssetsGroup *)group
{
    [self.images removeAllObjects];
    ALAssetsGroupEnumerationResultsBlock groupEnumerAtion =
    ^(ALAsset *result,NSUInteger index, BOOL *stop)
    {
        if (result!=NULL)
        {
            if ([[result valueForProperty:ALAssetPropertyType]isEqualToString:ALAssetTypePhoto])
            {
                [self.images addObject:result];
            }
        }
        else
        {
            //主线程中刷新UI
        }
        
    };
    [group enumerateAssetsUsingBlock:groupEnumerAtion];
}


  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS开发中上传图片可以采用以下步骤: 1.选择要上传的图片,可以使用系统提供的UIImagePickerController控制器,或者使用第三方库,例如TZImagePickerController。 2.将选中的图片转换为NSData格式。 3.使用NSURLSession或AFNetworking等网络库,将图片数据上传到服务器。 以下是一个简单的上传图片的示例代码: ``` // 选择图片 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.delegate = self; [self presentViewController:imagePicker animated:YES completion:nil]; // 将选中的图片转换为NSData格式 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info { UIImage *selectedImage = info[UIImagePickerControllerOriginalImage]; NSData *imageData = UIImageJPEGRepresentation(selectedImage, 0.5); // 上传图片到服务器 NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; NSURL *url = [NSURL URLWithString:@"http://example.com/upload.php"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:imageData completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { // 处理服务器返回的响应 }]; [uploadTask resume]; [picker dismissViewControllerAnimated:YES completion:nil]; } ``` 其中,upload.php是服务器端接收图片的脚本文件。在服务器端,可以使用PHP等语言来处理上传的图片数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值