您的位置:首页 > 其它

从相册读取大文件dataWithContentsOfURL?

2017-09-29 17:25 1406 查看


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


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

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

Declaration

+ (instancetype)dataWithContentsOfURL:(NSURL *)url;
[/code]

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:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: