从相册读取大文件dataWithContentsOfURL?

dataWithContentsOfURL: 这个方法只能读取小点的文件,如果需要读取大文件,

比如几个G的,就需要用这种方式inputStreamWithURL:.


Returns a data object containing the data from the location specified by a given URL.


Declaration

+ (instancetype)dataWithContentsOfURL:(NSURL *)url;

Parameters

aURL

The URL from which to read data.

Return Value

A data object containing the data from the location specified by aURL. Returns nil if the data object could not be created.

Discussion

Use this method to converting data:// URLs to NSData objects. You can also use it to read short files synchronously. If you need to read potentially large files, use inputStreamWithURL: to open a stream, then read the file incrementally.

If you need to know the reason for failure, use dataWithContentsOfURL:options:error:.

参考地址:https://developer.apple.com/documentation/foundation/nsdata/1547245-datawithcontentsofurl



没怎么研究视频播放器,就在网上找了个,链接:https://github.com/835239104/KrVideoPlayerPlus

下载AFNetworking,

1.下载最新版的会报错,没有引入相关库

2.AFSecurityPolicy.m中:注释了一部分代码

#pragma mark - 这里改了 ------
#warning - 这里改了 --------
//#if !TARGET_OS_IOS && !TARGET_OS_WATCH
//static NSData * AFSecKeyGetData(SecKeyRef key) {
//    CFDataRef data = NULL;
//
//    __Require_noErr_Quiet(SecItemExport(key, kSecFormatUnknown, kSecItemPemArmour, NULL, &data), _out);
//
//    return (__bridge_transfer NSData *)data;
//
//_out:
//    if (data) {
//        CFRelease(data);
//    }
//
//    return nil;
//}
//#endif

static BOOL AFSecKeyIsEqualToKey(SecKeyRef key1, SecKeyRef key2) {
//#if TARGET_OS_IOS || TARGET_OS_WATCH
    return [(__bridge id)key1 isEqual:(__bridge id)key2];
//#else
//    return [AFSecKeyGetData(key1) isEqual:AFSecKeyGetData(key2)];
//#endif
}

3.实现边下边播(我用的是一边在网上看一边下载,所以会有点卡)

/**
 * 下载文件
 */
- (void)downloadFileURL:(NSString *)aUrl savePath:(NSString *)aSavePath fileName:(NSString *)aFileName tag:(NSInteger)aTag
{
    NSFileManager *fileManager = [NSFileManager defaultManager];

    //检查本地文件是否已存在
    NSString *fileName = [NSString stringWithFormat:@"%@/%@", aSavePath, aFileName];

    //检查附件是否存在
    if ([fileManager fileExistsAtPath:fileName]) {
         [self addVideoPlayerWithURL:[NSURL fileURLWithPath:fileName]];
    }else{
        //创建附件存储目录
        if (![fileManager fileExistsAtPath:aSavePath]) {
            [fileManager createDirectoryAtPath:aSavePath withIntermediateDirectories:YES attributes:nil error:nil];
        }
//         [self addVideoPlayerWithURL:[NSURL fileURLWithPath:fileName]];
        [self addVideoPlayerWithURL:[NSURL URLWithString:aUrl]];
        //下载附件
        NSURL *url = [[NSURL alloc] initWithString:aUrl];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];

        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        operation.inputStream   = [NSInputStream inputStreamWithURL:url];
        operation.outputStream  = [NSOutputStream outputStreamToFileAtPath:fileName append:NO];

        //下载进度控制

         [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
         NSLog(@"is download:%f", (float)totalBytesRead/totalBytesExpectedToRead);
         }];

        //已完成下载
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//            NSData *audioData = [NSData dataWithContentsOfFile:fileName];
            NSLog(@"完成下载");
            //设置下载数据到res字典对象中并用代理返回下载数据NSData
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"下载失败");
            //下载失败
        }];

        [operation start];
    }
}

注意:从本度读取路径:[NSURL fileURLWithPath:fileName]

在网上读取路径:[NSURL URLWithString:aUrl]

Demo百度云下载链接:http://pan.baidu.com/s/1c0bL9dQ

补充:iOS边下边播放 http://blog.csdn.net/zttjhm/article/details/38063605

iOS视频压缩:http://blog.csdn.net/lookyou111/article/details/25625775

Similar Posts:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值