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;

    }

  • 相关阅读:
    判断Java Bean对象所有属性是否为空
    Java在服务器下删除文件、删除目录及目录下所有文件失败(file.delete() --> false)
    Java判断两个Integer类型的值是否相等
    Java将多张图片合并打包成zip格式进行下载
    Java给图片添加水印部署到服务器出现中文乱码
    SpringBoot+SpringMvc页面跳转404问题
    已解决:Caused by: java.lang.NoClassDefFoundError: org/springframework/util/AntPathMatcher$AntPatternComparator$PatternInfo
    ActiveMQ如何解决被重复消费和数据丢失的问题?
    高并发系统三大利器之降级
    高并发系统三大利器之限流
  • 原文地址:https://www.cnblogs.com/xia0huihui/p/5430782.html
Copyright © 2011-2022 走看看