UIImageView+ZFCache.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // UIImageView+ZFCache.h
  3. // ZFPlayer
  4. //
  5. // Copyright (c) 2016年 任子丰 ( http://github.com/renzifeng )
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #import <UIKit/UIKit.h>
  25. typedef void (^ZFDownLoadDataCallBack)(NSData *data, NSError *error);
  26. typedef void (^ZFDownloadProgressBlock)(unsigned long long total, unsigned long long current);
  27. @interface ZFImageDownloader : NSObject<NSURLSessionDownloadDelegate>
  28. @property (nonatomic, strong) NSURLSession *session;
  29. @property (nonatomic, strong) NSURLSessionDownloadTask *task;
  30. @property (nonatomic, assign) unsigned long long totalLength;
  31. @property (nonatomic, assign) unsigned long long currentLength;
  32. @property (nonatomic, copy) ZFDownloadProgressBlock progressBlock;
  33. @property (nonatomic, copy) ZFDownLoadDataCallBack callbackOnFinished;
  34. - (void)startDownloadImageWithUrl:(NSString *)url
  35. progress:(ZFDownloadProgressBlock)progress
  36. finished:(ZFDownLoadDataCallBack)finished;
  37. @end
  38. typedef void (^ZFImageBlock)(UIImage *image);
  39. @interface UIImageView (ZFCache)
  40. /**
  41. * Get/Set the callback block when download the image finished.
  42. *
  43. * The image object from network or from disk.
  44. */
  45. @property (nonatomic, copy) ZFImageBlock completion;
  46. /**
  47. * Image downloader
  48. */
  49. @property (nonatomic, strong) ZFImageDownloader *imageDownloader;
  50. /**
  51. * Specify the URL to download images fails, the number of retries, the default is 2
  52. */
  53. @property (nonatomic, assign) NSUInteger attemptToReloadTimesForFailedURL;
  54. /**
  55. * Will automatically download to cutting for UIImageView size of image.The default value is NO.
  56. * If set to YES, then the download after a successful store only after cutting the image
  57. */
  58. @property (nonatomic, assign) BOOL shouldAutoClipImageToViewSize;
  59. /**
  60. * Set the imageView `image` with an `url` and a placeholder.
  61. *
  62. * The download is asynchronous and cached.
  63. *
  64. * @param url The url for the image.
  65. * @param placeholderImageName The image name to be set initially, until the image request finishes.
  66. */
  67. - (void)setImageWithURLString:(NSString *)url placeholderImageName:(NSString *)placeholderImageName;
  68. /**
  69. * Set the imageView `image` with an `url` and a placeholder.
  70. *
  71. * The download is asynchronous and cached.
  72. *
  73. * @param url The url for the image.
  74. * @param placeholderImage The image to be set initially, until the image request finishes.
  75. */
  76. - (void)setImageWithURLString:(NSString *)url placeholder:(UIImage *)placeholderImage;
  77. /**
  78. * Set the imageView `image` with an `url`, placeholder.
  79. *
  80. * The download is asynchronous and cached.
  81. *
  82. * @param url The url for the image.
  83. * @param placeholderImage The image to be set initially, until the image request finishes.
  84. * @param completion A block called when operation has been completed. This block has no return value
  85. * and takes the requested UIImage as first parameter. In case of error the image parameter
  86. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  87. * indicating if the image was retrieved from the local cache or from the network.
  88. * The fourth parameter is the original image url.
  89. */
  90. - (void)setImageWithURLString:(NSString *)url
  91. placeholder:(UIImage *)placeholderImage
  92. completion:(void (^)(UIImage *image))completion;
  93. /**
  94. * Set the imageView `image` with an `url`, placeholder.
  95. *
  96. * The download is asynchronous and cached.
  97. *
  98. * @param url The url for the image.
  99. * @param placeholderImageName The image name to be set initially, until the image request finishes.
  100. * @param completion A block called when operation has been completed. This block has no return value
  101. * and takes the requested UIImage as first parameter. In case of error the image parameter
  102. * is nil and the second parameter may contain an NSError. The third parameter is a Boolean
  103. * indicating if the image was retrieved from the local cache or from the network.
  104. * The fourth parameter is the original image url.
  105. */
  106. - (void)setImageWithURLString:(NSString *)url
  107. placeholderImageName:(NSString *)placeholderImageName
  108. completion:(void (^)(UIImage *image))completion;
  109. @end