ALAssetsLibrary从相册 获取视频

/// 取视频

-(void)getAllVideo

{

    library = [[ALAssetsLibrary alloc] init];

    //监测是否可用

    ALAssetsLibraryAccessFailureBlock failureblock =

    ^(NSError *myerror)

    {

        NSLog(@"相册访问失败 =%@", [myerror localizedDescription]);

        if ([myerror.localizedDescription rangeOfString:@"Global denied access"].location!=NSNotFound) {

            NSLog(@"无法访问相册.请在'设置->定位服务'设置为打开状态.");

        }else{

            NSLog(@"相册访问失败.");

        }

   /// 不允许访问的UI提示

        [self createNotAllowView];

        return ;

    };

    mutableArray =[[NSMutableArray alloc]init];

    ALAssetsLibraryGroupsEnumerationResultsBlock

    libraryGroupsEnumeration = ^(ALAssetsGroup* group,BOOL* stop){

        if (group != nil)

        {

    /// 只取视频

            [group setAssetsFilter:[ALAssetsFilter allVideos]];

            [group enumerateAssetsWithOptions:NSEnumerationConcurrent usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {

                if (result) {

                    [mutableArray addObject:result];

                }

            }];

        }

        else

        {

            NSLog(@"%ld",mutableArray.count);

            [_collectionView reloadData];

        }

    };

    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos

                                                         usingBlock:libraryGroupsEnumeration

                                                       failureBlock:failureblock];

}

/// 写入本地

-(void)handleWrittenFile:(ALAsset*) videoAsset

{

    if (!videoAsset) {

        NSLog(@"nil");

        return;

    }

  /// 写入本地路径 

    NSString *fileName = [self getVideoMergeFilePathString];

    ALAssetRepresentation *represent = [videoAsset defaultRepresentation];

    NSUInteger size = [represent size];

  /// 开辟65636大小的内存 一次读取和写入 

    const int bufferSize = 65636;

    NSLog(@"written to : %@", fileName);

    FILE *fileOpen = fopen([fileName cStringUsingEncoding:1], "wb+");

    if (fileOpen == NULL) {

        return;

    }

    Byte *buffer =(Byte*)malloc(bufferSize);

    NSUInteger read =0, offset = 0;

    NSError *error;

    if (size != 0) {

        do {

            read = [represent getBytes:buffer

                      fromOffset:offset

                          length:bufferSize

                           error:&error];

            fwrite(buffer, sizeof(char), read, fileOpen);

            offset += read;

        } while (read != 0);

    }

    free(buffer);

    buffer = NULL;

    fclose(fileOpen);

    fileOpen= NULL;

}

原文地址:https://www.cnblogs.com/xia0huihui/p/5430782.html