VLCNetworkImageView.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*****************************************************************************
  2. * VLC for iOS
  3. *****************************************************************************
  4. * Copyright (c) 2015 VideoLAN. All rights reserved.
  5. * $Id$
  6. *
  7. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. #import "VLCNetworkImageView.h"
  12. @implementation VLCNetworkImageView
  13. static NSCache *sharedImageCache = nil;
  14. + (void)setSharedImageCache:(NSCache *)sharedCache {
  15. sharedImageCache = sharedCache;
  16. }
  17. + (NSCache *)sharedImageCache {
  18. if (!sharedImageCache) {
  19. sharedImageCache = [[NSCache alloc] init];
  20. [sharedImageCache setCountLimit:50];
  21. }
  22. return sharedImageCache;
  23. }
  24. - (UIImage *)cacheImageForURL:(NSURL *)url {
  25. UIImage *image = [[self.class sharedImageCache] objectForKey:url];
  26. if ((image != nil) && [image isKindOfClass:[UIImage class]]) {
  27. return image;
  28. }
  29. return nil;
  30. }
  31. - (void)cancelLoading {
  32. [self.downloadTask cancel];
  33. self.downloadTask = nil;
  34. }
  35. - (void)setImageWithURL:(NSURL *)url {
  36. [self cancelLoading];
  37. UIImage *cachedImage = [self cacheImageForURL:url];
  38. if (cachedImage) {
  39. self.image = cachedImage;
  40. } else {
  41. __weak typeof(self) weakSelf = self;
  42. NSURLSession *sharedSession = [NSURLSession sharedSession];
  43. self.downloadTask = [sharedSession dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  44. if (!data) {
  45. return;
  46. }
  47. UIImage *image = [UIImage imageWithData:data];
  48. if (!image) { return; }
  49. [[[weakSelf class] sharedImageCache] setObject:image forKey:url];
  50. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  51. __strong typeof(weakSelf) strongSelf = weakSelf;
  52. if ([strongSelf.downloadTask.originalRequest.URL isEqual:url]) {
  53. if (strongSelf.animateImageSetting) {
  54. [UIView animateWithDuration:.3 animations:^{
  55. strongSelf.image = image;
  56. }];
  57. } else
  58. strongSelf.image = image;
  59. strongSelf.downloadTask = nil;
  60. }
  61. }];
  62. }];
  63. [self.downloadTask resume];
  64. }
  65. }
  66. - (void)setImage:(UIImage *)image
  67. {
  68. [super setImage:image];
  69. [self setNeedsUpdateConstraints];
  70. [self invalidateIntrinsicContentSize];
  71. }
  72. - (void)updateConstraints
  73. {
  74. [super updateConstraints];
  75. CGSize size = self.image.size;
  76. if (self.aspectRatioConstraint && size.height && size.width) {
  77. NSLayoutConstraint *newConstraint = [NSLayoutConstraint constraintWithItem:self
  78. attribute:NSLayoutAttributeWidth
  79. relatedBy:NSLayoutRelationEqual
  80. toItem:self
  81. attribute:NSLayoutAttributeHeight
  82. multiplier:size.width/size.height
  83. constant:0];
  84. [self removeConstraint:self.aspectRatioConstraint];
  85. [self addConstraint:newConstraint];
  86. self.aspectRatioConstraint = newConstraint;
  87. }
  88. }
  89. @end