码迷,mamicode.com
首页 > 其他好文 > 详细

从AssetLibrary获取视频流并保存到文件

时间:2016-03-10 22:04:38      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
    [library enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        [group setAssetsFilter:[ALAssetsFilter allVideos]];
        if (group.numberOfAssets > 0)
        {
            NSIndexSet *videoSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, group.numberOfAssets)];
            [group enumerateAssetsAtIndexes:videoSet options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                ALAssetRepresentation *representation = [result defaultRepresentation];
                NSString *savingPath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@",representation.filename];
                [[NSFileManager defaultManager]createFileAtPath:savingPath contents:nil attributes:nil];
                NSFileHandle *writingHandle = [NSFileHandle fileHandleForWritingAtPath:savingPath];
                int bufferSize = 1024;
                uint8_t buffer[bufferSize];
                unsigned long long read = 0,length = 0;
                NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
                int n = 0;
                for (;read < representation.size;)
                {
                    if (n % 10 == 0)
                    {
                        [pool release];
                        pool = nil;
                        pool = [[NSAutoreleasePool alloc]init];
                    }
                    length = [representation getBytes:buffer fromOffset:read length:bufferSize error:nil];
                    read += length;
                    NSData *fileData = [NSData dataWithBytes:(const void *)buffer length:(NSUInteger)length];
                    [writingHandle writeData:fileData];
                    n++;
                }
                [writingHandle closeFile];
                [pool drain];
            }];
        }
    } failureBlock:^(NSError *error) {
  
    }];

 

从AssetLibrary获取视频流并保存到文件

标签:

原文地址:http://www.cnblogs.com/yan-chao-zhi/p/5263507.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!