forked from NSFish/QiNiuALAssetSupport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QNResumeUpload+ALAssetSupport.m
94 lines (76 loc) · 3.14 KB
/
QNResumeUpload+ALAssetSupport.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// QNResumeUpload+ALAssetSupport.m
// QiChengNew
//
// Created by 乐星宇 on 14/11/14.
// Copyright (c) 2014年 奇橙百优. All rights reserved.
//
#import "QNResumeUpload+ALAssetSupport.h"
#import "QNHttpManager.h"
#import "QNCrc32.h"
#import "QNConfig.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <objc/message.h>
#import <objc/runtime.h>
static char kAssetKey;
@implementation QNResumeUpload (ALAssetSupport)
@dynamic asset;
- (void)makeBlock:(NSString *)uphost
offset:(UInt32)offset
blockSize:(UInt32)blockSize
chunkSize:(UInt32)chunkSize
progress:(QNInternalProgressBlock)progressBlock
complete:(QNCompleteBlock)complete
{
NSData *data = [self dataFromALAssetAtOffset:offset size:chunkSize];//[self.data subdataWithRange:NSMakeRange(offset, (unsigned int)chunkSize)];
NSString *url = [[NSString alloc] initWithFormat:@"http://%@/mkblk/%u", uphost, (unsigned int)blockSize];
UInt32 crc = [QNCrc32 data:data];
[self setChunkCrcValue:crc];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
SEL selector = @selector(post:withData:withCompleteBlock:withProgressBlock:);
#pragma clang diagnostic pop
void (*typed_msgSend)(id, SEL, NSString *, NSData *, QNCompleteBlock, QNInternalProgressBlock) = (void *)objc_msgSend;
typed_msgSend(self, selector, url, data, complete, progressBlock);
}
- (void)putChunk:(NSString *)uphost
offset:(UInt32)offset
size:(UInt32)size
context:(NSString *)context
progress:(QNInternalProgressBlock)progressBlock
complete:(QNCompleteBlock)complete {
NSData *data = [self dataFromALAssetAtOffset:offset size:size];//[self.data subdataWithRange:NSMakeRange(offset, (unsigned int)size)];
UInt32 chunkOffset = offset % kQNBlockSize;
NSString *url = [[NSString alloc] initWithFormat:@"http://%@/bput/%@/%u", uphost, context, (unsigned int)chunkOffset];
UInt32 crc = [QNCrc32 data:data];
[self setChunkCrcValue:crc];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
SEL selector = @selector(post:withData:withCompleteBlock:withProgressBlock:);
#pragma clang diagnostic pop
void (*typed_msgSend)(id, SEL, NSString *, NSData *, QNCompleteBlock, QNInternalProgressBlock) = (void *)objc_msgSend;
typed_msgSend(self, selector, url, data, complete, progressBlock);
}
#pragma mark 读取ALAsset中的data
- (NSData *)dataFromALAssetAtOffset:(NSInteger)offset size:(NSInteger)size
{
ALAssetRepresentation *rep = [self.asset defaultRepresentation];
Byte *buffer = (Byte *)malloc(size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:offset length:size error:nil];
return [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
}
#pragma mark CRC
- (void)setChunkCrcValue:(UInt32)crc
{
[self setValue:@(crc) forKey:@"chunkCrc"];
}
#pragma mark Asset setter and getter
- (void)setAsset:(ALAsset *)asset
{
objc_setAssociatedObject(self, &kAssetKey, asset, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (ALAsset *)asset
{
return objc_getAssociatedObject(self, &kAssetKey);
}
@end