QNALAssetFile.m 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // QNALAssetFile.m
  3. // QiniuSDK
  4. //
  5. // Created by bailong on 15/7/25.
  6. // Copyright (c) 2015年 Qiniu. All rights reserved.
  7. //
  8. #import "QNALAssetFile.h"
  9. #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
  10. #import <AssetsLibrary/AssetsLibrary.h>
  11. #import "QNResponseInfo.h"
  12. @interface QNALAssetFile ()
  13. @property (nonatomic) ALAsset *asset;
  14. @property (readonly) int64_t fileSize;
  15. @property (readonly) int64_t fileModifyTime;
  16. @end
  17. @implementation QNALAssetFile
  18. - (instancetype)init:(ALAsset *)asset
  19. error:(NSError *__autoreleasing *)error {
  20. if (self = [super init]) {
  21. NSDate *createTime = [asset valueForProperty:ALAssetPropertyDate];
  22. int64_t t = 0;
  23. if (createTime != nil) {
  24. t = [createTime timeIntervalSince1970];
  25. }
  26. _fileModifyTime = t;
  27. _fileSize = asset.defaultRepresentation.size;
  28. _asset = asset;
  29. }
  30. return self;
  31. }
  32. - (NSData *)read:(long)offset
  33. size:(long)size {
  34. ALAssetRepresentation *rep = [self.asset defaultRepresentation];
  35. Byte *buffer = (Byte *)malloc(size);
  36. NSUInteger buffered = [rep getBytes:buffer fromOffset:offset length:size error:nil];
  37. return [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
  38. }
  39. - (NSData *)readAll {
  40. return [self read:0 size:(long)_fileSize];
  41. }
  42. - (void)close {
  43. }
  44. - (NSString *)path {
  45. ALAssetRepresentation *rep = [self.asset defaultRepresentation];
  46. return [rep url].path;
  47. }
  48. - (int64_t)modifyTime {
  49. return _fileModifyTime;
  50. }
  51. - (int64_t)size {
  52. return _fileSize;
  53. }
  54. @end
  55. #endif