zoukankan      html  css  js  c++  java
  • 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;

    }

  • 相关阅读:
    地图 SDK 系列教程-在地图上展示指定区域
    [奇思妙想]下一个和微博、微信同级别的应用为是什么样的
    [办公自动化]EXCEL不大,但是保存很慢
    [奇思妙想]公共图书馆+快递
    [奇思妙想]“停哪了”
    [IT学习]阿铭Linux 微信公众号 每日一题 解析
    [IT学习]GIT 学习
    [故障处理]西部数据wd elements xp 无法识别
    [奇思妙想]无人机
    [IT学习]跟阿铭学linux(第3版)
  • 原文地址:https://www.cnblogs.com/xia0huihui/p/5430782.html
Copyright © 2011-2022 走看看