-
Notifications
You must be signed in to change notification settings - Fork 0
/
WriteFileWithAsset.m
74 lines (63 loc) · 2.08 KB
/
WriteFileWithAsset.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// NSTimer+Blocks.m
// WangPan
//
// Created by riven on 13-11-29.
// Copyright (c) 2013年 sohu. All rights reserved.
//
#import "WriteFileWithAsset.h"
@implementation WriteFileWithAsset
+ (void)writeFileToPath:(NSString*)filePath andAsset:(ALAsset*)asset
{
ALAssetRepresentation * rep = [asset defaultRepresentation];
NSUInteger size = [rep size];
const int bufferSize = 1024*1024; // or use 8192 size as read from other posts
NSLog(@"Writing to %@",filePath);
FILE* f = fopen([filePath cStringUsingEncoding:1], "wb+");
if (f == NULL) {
NSLog(@"Can not create tmp file.");
return;
}
Byte * buffer = (Byte*)malloc(bufferSize);
int read = 0, offset = 0, written = 0;
NSError* err;
if (size != 0) {
do {
read = [rep getBytes:buffer
fromOffset:offset
length:bufferSize
error:&err];
written = fwrite(buffer, sizeof(char), read, f);
NSLog(@"written==%d",written);
offset += read;
} while (read != 0);
}
fclose(f);
free(buffer);
return ;
}
+ (BOOL)writeDataToPath:(NSString*)filePath andAsset:(ALAsset*)asset
{
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];
if (!handle) {
return NO;
}
static const NSUInteger BufferSize = 1024*1024;
ALAssetRepresentation *rep = [asset defaultRepresentation];
uint8_t *buffer = calloc(BufferSize, sizeof(*buffer));
NSUInteger offset = 0, bytesRead = 0;
do {
@try {
bytesRead = [rep getBytes:buffer fromOffset:offset length:BufferSize error:nil];
[handle writeData:[NSData dataWithBytesNoCopy:buffer length:bytesRead freeWhenDone:NO]];
offset += bytesRead;
} @catch (NSException *exception) {
free(buffer);
return NO;
}
} while (bytesRead > 0);
free(buffer);
return YES;
}
@end