zoukankan      html  css  js  c++  java
  • 相册视频保存进沙盒

    url为相册中视频对应的路径

     

    // 将原始视频的URL转化为NSData数据,写入沙盒

    - (void)videoWithUrl:(NSURL *)url withFileName:(NSString *)fileName andVideoUrlPath:(NSString *)KVideoUrlPath{

        

        NSFileManager * fileManager = [NSFileManager defaultManager];

        if (![fileManager fileExistsAtPath:KVideoUrlPath]) {

            [fileManager createDirectoryAtPath:KVideoUrlPath withIntermediateDirectories:YES attributes:nil error:nil];

        }

        ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            if (url) {

                [assetLibrary assetForURL:url resultBlock:^(ALAsset *asset) {

                    ALAssetRepresentation *rep = [asset defaultRepresentation];

                    NSString * videoPath = [KVideoUrlPath stringByAppendingPathComponent:fileName];

                    const char *cvideoPath = [videoPath UTF8String];

                    FILE *file = fopen(cvideoPath, "a+");

                    if (file) {

                        const int bufferSize = 111024 * 1024;

                        // 初始化一个1Mbuffer

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

                        NSUInteger read = 0, offset = 0, written = 0;

                        NSError* err = nil;

                        if (rep.size != 0)

                        {

                            do {

                                read = [rep getBytes:buffer fromOffset:offset length:bufferSize error:&err];

                                written = fwrite(buffer, sizeof(char), read, file);

                                offset += read;

                            } while (read != 0 && !err);

                        }

                        // 释放缓冲区,关闭文件

                        free(buffer);

                        buffer = NULL;

                        fclose(file);

                        file = NULL;

     

                    }

                } failureBlock:nil];

            }

        });

    }

     

    http://www.jianshu.com/p/1eeaec2ae0fa

    http://blog.csdn.net/wsyx768/article/details/50771993

    http://www.jianshu.com/p/29585a416af2

  • 相关阅读:
    Java8中findAny和findFirst的区别
    Lombok使用与原理
    java.util.ConcurrentModificationException异常原因及解决方法
    PacketTooBigException问题解决
    数据库中空字符串和NULL值两个概念的区别
    Java8采用stream、parallelStream迭代的区别
    Spring四大注解
    @Qualifier的作用和应用
    @resource和@autowired的区别是什么
    @Transactional注解详细用法
  • 原文地址:https://www.cnblogs.com/wskgjmhh/p/6873682.html
Copyright © 2011-2022 走看看