TZImageRequestOperation.m 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // TZImageRequestOperation.m
  3. // TZImagePickerControllerFramework
  4. //
  5. // Created by 谭真 on 2018/12/20.
  6. // Copyright © 2018 谭真. All rights reserved.
  7. //
  8. #import "TZImageRequestOperation.h"
  9. #import "TZImageManager.h"
  10. @implementation TZImageRequestOperation
  11. @synthesize executing = _executing;
  12. @synthesize finished = _finished;
  13. - (instancetype)initWithAsset:(PHAsset *)asset completion:(TZImageRequestCompletedBlock)completionBlock progressHandler:(TZImageRequestProgressBlock)progressHandler {
  14. self = [super init];
  15. self.asset = asset;
  16. self.completedBlock = completionBlock;
  17. self.progressBlock = progressHandler;
  18. _executing = NO;
  19. _finished = NO;
  20. return self;
  21. }
  22. - (void)start {
  23. self.executing = YES;
  24. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  25. [[TZImageManager manager] getPhotoWithAsset:self.asset completion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) {
  26. dispatch_async(dispatch_get_main_queue(), ^{
  27. if (!isDegraded) {
  28. if (self.completedBlock) {
  29. self.completedBlock(photo, info, isDegraded);
  30. }
  31. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  32. [self done];
  33. });
  34. }
  35. });
  36. } progressHandler:^(double progress, NSError *error, BOOL *stop, NSDictionary *info) {
  37. dispatch_async(dispatch_get_main_queue(), ^{
  38. if (self.progressBlock) {
  39. self.progressBlock(progress, error, stop, info);
  40. }
  41. });
  42. } networkAccessAllowed:YES];
  43. });
  44. }
  45. - (void)done {
  46. self.finished = YES;
  47. self.executing = NO;
  48. [self reset];
  49. }
  50. - (void)reset {
  51. self.asset = nil;
  52. self.completedBlock = nil;
  53. self.progressBlock = nil;
  54. }
  55. - (void)setFinished:(BOOL)finished {
  56. [self willChangeValueForKey:@"isFinished"];
  57. _finished = finished;
  58. [self didChangeValueForKey:@"isFinished"];
  59. }
  60. - (void)setExecuting:(BOOL)executing {
  61. [self willChangeValueForKey:@"isExecuting"];
  62. _executing = executing;
  63. [self didChangeValueForKey:@"isExecuting"];
  64. }
  65. - (BOOL)isAsynchronous {
  66. return YES;
  67. }
  68. @end