iOS通过AssetsLibrary获取相册内的图片和视频和写入本地

开发中遇到UI出图 获取本地相册的所有图片和视频 选择上传
所以选择了AssetsLibrary来获取本地相册里的东西
首先初始化ALAssetsLibrary

@property (nonatomic, strong) ALAssetsLibrary *library;

self.library = [[ALAssetsLibrary alloc] init];

PS:这里切勿

PHPhotoLibrary *lib = [[PHPhotoLibrary alloc]init];

因为PHPhotoLibrary的实例方法说明了 出了实例的生命周期 所有的东西都会被置为nil

// Get the list of groups that match the given types. Multiple types can be ORed together. The results are passed one by one to the caller by executing the enumeration block.
// When the enumeration is done, 'enumerationBlock' will be called with group set to nil.
// When groups are enumerated, the user may be asked to confirm the application's access to the data. If the user denies access to the application or if no application is allowed to access the data, the failure block will be called.
// If the data is currently unavailable, the failure block will be called.
- (void)enumerateGroupsWithTypes:(ALAssetsGroupType)types usingBlock:(ALAssetsLibraryGroupsEnumerationResultsBlock)enumerationBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock NS_DEPRECATED_IOS(4_0, 9_0, "Use the PHFetchResult returned by one of the fetch... methods on PHAssetCollection from the Photos framework to enumerate asset collections instead");

初始化好了之后 直接用上面的这个方法查找出相册的分组 也就是group

然后再去遍历每个gruop 拿到ALAsset ALAsset中有个 ALAssetRepresentation的对象defaultRepresentation 通过defaultRepresentation我们可以拿到资源的路径等等

[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                @autoreleasepool{
                    if (result) {
                      //这是我自己的model
                        TYSeleteModel *model = [[TYSeleteModel alloc] init];
                        NSURL *url = [result valueForProperty:ALAssetPropertyAssetURL];
                        if ([url.absoluteString containsString:@"MP4"] || [url.absoluteString containsString:@"MOV"]){
                            NSURL *url = [[result defaultRepresentation] url];
                            //获取视频的时长
                            AVURLAsset * asset = [AVURLAsset assetWithURL:url];
                            CMTime   time = [asset duration];
                            int seconds = ceil(time.value/time.timescale);
                            model.time = seconds;
                            model.imageURL = result.defaultRepresentation.url;
                        }
                      //缩略图
                        UIImage *image = [UIImage imageWithCGImage:[result thumbnail]];
                        model.image = image;
                      //存defaultRepresentation对象  
                        model.defaultRepresentation = result.defaultRepresentation;
                        [self.dataList insertObject:model atIndex:0];
                    }
                }
                
            }];

我的本地model是这样的

@interface TYSeleteModel : NSObject

@property (nonatomic, strong) UIImage *image;

@property (nonatomic, assign) BOOL isSelect;

//时长  为0代表是图片  
@property (nonatomic, assign) NSInteger time;

@property (nonatomic, strong) ALAssetRepresentation *defaultRepresentation;

@property (nonatomic,copy) NSURL *imageURL;//原图url,是AssetURL

@end

这样就获取到了本地相册的相片和视频 然后通过collectionView展示选择即可

··········································分割线·······································
然后用户选择完毕之后 需要上传图片和视频 以NSData的形式进行上传 我们就需要获取到资源 转成NSData进行上传 这里我选择的是 把文件写入本地指定路径 然后通过路径获取文件进行上传
首先获取沙盒路径 我指定文件夹uploadFile

    NSString *basePath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,  NSUserDomainMask, YES).firstObject;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *directryPath = [basePath stringByAppendingPathComponent:@"uploadFile"];
    if (![fileManager fileExistsAtPath:directryPath]) {
        [fileManager createDirectoryAtPath:directryPath withIntermediateDirectories:YES attributes:nil error:nil];
    }

然后是图片的写入 注意 我们展示用的是缩略图 这里我们写入和上传的是高清图 通过model里的defaultRepresentation对象获取高清图

                    CGImageRef fullScreenRef = [model.defaultRepresentation fullResolutionImage];
                    //                UIImage *fullScreenImg = [[UIImage alloc]initWithCGImage:fullScreenRef];
                    UIImage *fullScreenImg = [[UIImage alloc] initWithCGImage:model.defaultRepresentation.fullResolutionImage scale:1 orientation:model.defaultRepresentation.orientation];
                    NSData *data = UIImagePNGRepresentation(fullScreenImg);
                  //  NSString *subPath = [NSString stringWithFormat:@"%d-%@-%lu.jpg",self.parentModel.fileId,[self getCurrentTimes],(unsigned long)[seleteArr indexOfObject:model]];
                 //   NSString *path = [NSString stringWithFormat:@"%@/uploadFile/%@",basePath,subPath];
                    if ([data writeToFile:path atomically:YES]) {
                        NSLog(@"写入图片成功 路径为 %@",path);
                        dispatch_async(dispatch_get_main_queue(), ^{
                            //加入上传队列
                            [[TYShareCloudDownloadAndUploadHelper shareInstance] addUploadTaskWithParentID:ID fileName:subPath fileType:1 fileSize:data.length / 1024];
                        });
                    }

然后是视频的写入本地 先要对视频进行转码 输出到指定路径 然后通过dataWithContentsOfFile转成NSData

                    //视频
                    NSString *subPath = [NSString stringWithFormat:@"%d-%@-%lu.mp4",self.parentModel.fileId,[self getCurrentTimes],(unsigned long)[seleteArr indexOfObject:model]];
                    NSString *path = [NSString stringWithFormat:@"%@/uploadFile/%@",basePath,subPath];
                    //转码配置
                    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:model.imageURL options:nil];
                    //AVAssetExportPresetMediumQuality可以更改,是枚举类型,官方有提供,更改该值可以改变视频的压缩比例
                    AVAssetExportSession *exportSession= [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
                    exportSession.shouldOptimizeForNetworkUse = YES;
                    exportSession.outputURL = [NSURL fileURLWithPath:path];
                    //AVFileTypeMPEG4 文件输出类型,可以更改,是枚举类型,官方有提供,更改该值也可以改变视频的压缩比例
                    exportSession.outputFileType = AVFileTypeMPEG4;
                    [exportSession exportAsynchronouslyWithCompletionHandler:^{
                        int exportStatus = exportSession.status;
                        NSLog(@"%d",exportStatus);
                        switch (exportStatus)
                        {
                            case AVAssetExportSessionStatusFailed:
                            {
                                // log error to text view
                                NSError *exportError = exportSession.error;
                                NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError);
                                break;
                            }
                            case AVAssetExportSessionStatusCompleted:
                            {
                                NSLog(@"视频转码成功");
                                NSData *data = [NSData dataWithContentsOfFile:path];
                                if ([data writeToFile:path atomically:YES]) {
                                    NSLog(@"写入视频成功 路径为%@",path);
                                    dispatch_async(dispatch_get_main_queue(), ^{
                                      //加入上传队列
                                        [[TYShareCloudDownloadAndUploadHelper shareInstance] addUploadTaskWithParentID:ID fileName:subPath fileType:2 fileSize:data.length / 1024];
   
                                    });
                                }
                                
                            }
                        }
                    }];

然后上传之后本地文件记得删除

你可能感兴趣的:(iOS通过AssetsLibrary获取相册内的图片和视频和写入本地)