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;

    }

  • 相关阅读:
    mysql 远程连接数据库的二种方法
    安装mysql-5.7.12-winx64
    快速提升word文档编写质量
    查看linux系统版本命令汇总
    SpringMVC+mybatis+maven+Ehcache缓存实现
    linux下的java开发环境
    appium 常用api介绍(2)
    appium 常用api介绍(1)
    appium入门
    Mysql5.7服务下载安装
  • 原文地址:https://www.cnblogs.com/xia0huihui/p/5430782.html
Copyright © 2011-2022 走看看