MKAnnotationView+WebCache.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // MKAnnotationView+WebCache.m
  3. // SDWebImage
  4. //
  5. // Created by Olivier Poitrey on 14/03/12.
  6. // Copyright (c) 2012 Dailymotion. All rights reserved.
  7. //
  8. #import "MKAnnotationView+WebCache.h"
  9. #import "objc/runtime.h"
  10. #import "UIView+WebCacheOperation.h"
  11. static char imageURLKey;
  12. @implementation MKAnnotationView (WebCache)
  13. - (NSURL *)sd_imageURL {
  14. return objc_getAssociatedObject(self, &imageURLKey);
  15. }
  16. - (void)sd_setImageWithURL:(NSURL *)url {
  17. [self sd_setImageWithURL:url placeholderImage:nil options:0 completed:nil];
  18. }
  19. - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder {
  20. [self sd_setImageWithURL:url placeholderImage:placeholder options:0 completed:nil];
  21. }
  22. - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options {
  23. [self sd_setImageWithURL:url placeholderImage:placeholder options:options completed:nil];
  24. }
  25. - (void)sd_setImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock {
  26. [self sd_setImageWithURL:url placeholderImage:nil options:0 completed:completedBlock];
  27. }
  28. - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock {
  29. [self sd_setImageWithURL:url placeholderImage:placeholder options:0 completed:completedBlock];
  30. }
  31. - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock {
  32. [self sd_cancelCurrentImageLoad];
  33. objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  34. self.image = placeholder;
  35. if (url) {
  36. __weak MKAnnotationView *wself = self;
  37. id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
  38. if (!wself) return;
  39. dispatch_main_sync_safe(^{
  40. __strong MKAnnotationView *sself = wself;
  41. if (!sself) return;
  42. if (image) {
  43. sself.image = image;
  44. }
  45. if (completedBlock && finished) {
  46. completedBlock(image, error, cacheType, url);
  47. }
  48. });
  49. }];
  50. [self sd_setImageLoadOperation:operation forKey:@"MKAnnotationViewImage"];
  51. } else {
  52. dispatch_main_async_safe(^{
  53. NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
  54. if (completedBlock) {
  55. completedBlock(nil, error, SDImageCacheTypeNone, url);
  56. }
  57. });
  58. }
  59. }
  60. - (void)sd_cancelCurrentImageLoad {
  61. [self sd_cancelImageLoadOperationWithKey:@"MKAnnotationViewImage"];
  62. }
  63. @end
  64. @implementation MKAnnotationView (WebCacheDeprecated)
  65. - (NSURL *)imageURL {
  66. return [self sd_imageURL];
  67. }
  68. - (void)setImageWithURL:(NSURL *)url {
  69. [self sd_setImageWithURL:url placeholderImage:nil options:0 completed:nil];
  70. }
  71. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder {
  72. [self sd_setImageWithURL:url placeholderImage:placeholder options:0 completed:nil];
  73. }
  74. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options {
  75. [self sd_setImageWithURL:url placeholderImage:placeholder options:options completed:nil];
  76. }
  77. - (void)setImageWithURL:(NSURL *)url completed:(SDWebImageCompletedBlock)completedBlock {
  78. [self sd_setImageWithURL:url placeholderImage:nil options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  79. if (completedBlock) {
  80. completedBlock(image, error, cacheType);
  81. }
  82. }];
  83. }
  84. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock {
  85. [self sd_setImageWithURL:url placeholderImage:placeholder options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  86. if (completedBlock) {
  87. completedBlock(image, error, cacheType);
  88. }
  89. }];
  90. }
  91. - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock {
  92. [self sd_setImageWithURL:url placeholderImage:placeholder options:options completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  93. if (completedBlock) {
  94. completedBlock(image, error, cacheType);
  95. }
  96. }];
  97. }
  98. - (void)cancelCurrentImageLoad {
  99. [self sd_cancelCurrentImageLoad];
  100. }
  101. @end