|
1 | 1 | #import "WPMediaCollectionViewCell.h" |
2 | 2 |
|
| 3 | +static const NSTimeInterval ThredsholdForAnimation = 0.03; |
| 4 | +static const CGFloat TimeForFadeAnimation = 0.3; |
| 5 | + |
3 | 6 | @interface WPMediaCollectionViewCell () |
4 | 7 |
|
5 | 8 | @property (nonatomic, strong) UILabel *positionLabel; |
@@ -32,7 +35,11 @@ - (id)initWithCoder:(NSCoder *)aDecoder |
32 | 35 | - (void)prepareForReuse |
33 | 36 | { |
34 | 37 | [super prepareForReuse]; |
35 | | - [self setImage:nil]; |
| 38 | + if (self.tag != 0) { |
| 39 | + [self.asset cancelImageRequest:(WPMediaRequestID)self.tag]; |
| 40 | + } |
| 41 | + self.tag = 0; |
| 42 | + [self setImage:nil animated:NO]; |
36 | 43 | [self setCaption:@""]; |
37 | 44 | [self setPosition:NSNotFound]; |
38 | 45 | [self setSelected:NO]; |
@@ -72,28 +79,97 @@ - (void)commonInit |
72 | 79 | [self.contentView addSubview:_captionLabel]; |
73 | 80 | } |
74 | 81 |
|
75 | | -- (void)setImage:(UIImage *)image withAccessibilityLabel:(NSString*)accessibilityLabel |
| 82 | +- (void)setAsset:(id<WPMediaAsset>)asset { |
| 83 | + _asset = asset; |
| 84 | + __block WPMediaRequestID requestKey = 0; |
| 85 | + NSTimeInterval timestamp = [NSDate timeIntervalSinceReferenceDate]; |
| 86 | + requestKey = [_asset imageWithSize:self.frame.size completionHandler:^(UIImage *result, NSError *error) { |
| 87 | + BOOL animated = ([NSDate timeIntervalSinceReferenceDate] - timestamp) > ThredsholdForAnimation; |
| 88 | + if (error) { |
| 89 | + self.image = nil; |
| 90 | + NSLog(@"%@", [error localizedDescription]); |
| 91 | + return; |
| 92 | + } |
| 93 | + // Did this request changed meanwhile |
| 94 | + if (requestKey != self.tag) { |
| 95 | + return; |
| 96 | + } |
| 97 | + if ([NSThread isMainThread]){ |
| 98 | + [self setImage:result |
| 99 | + animated:animated]; |
| 100 | + } else { |
| 101 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 102 | + [self setImage:result |
| 103 | + animated:animated]; |
| 104 | + }); |
| 105 | + } |
| 106 | + }]; |
| 107 | + self.tag = requestKey; |
| 108 | + NSString *label = @""; |
| 109 | + NSString *caption = @""; |
| 110 | + WPMediaType assetType = _asset.assetType; |
| 111 | + switch (assetType) { |
| 112 | + case WPMediaTypeImage: |
| 113 | + label = [NSString stringWithFormat:NSLocalizedString(@"Image, %@", @"Accessibility label for image thumbnails in the media collection view. The parameter is the creation date of the image."), |
| 114 | + [[[self class] dateFormatter] stringFromDate:_asset.date]]; |
| 115 | + break; |
| 116 | + case WPMediaTypeVideo: |
| 117 | + label = [NSString stringWithFormat:NSLocalizedString(@"Video, %@", @"Accessibility label for video thumbnails in the media collection view. The parameter is the creation date of the video."), |
| 118 | + [[[self class] dateFormatter] stringFromDate:_asset.date]]; |
| 119 | + NSTimeInterval duration = [asset duration]; |
| 120 | + caption = [self stringFromTimeInterval:duration]; |
| 121 | + break; |
| 122 | + default: |
| 123 | + break; |
| 124 | + } |
| 125 | + self.imageView.accessibilityLabel = label; |
| 126 | + [self setCaption:caption]; |
| 127 | +} |
| 128 | + |
| 129 | ++ (NSDateFormatter *) dateFormatter { |
| 130 | + static NSDateFormatter *_dateFormatter = nil; |
| 131 | + static dispatch_once_t _onceToken; |
| 132 | + dispatch_once(&_onceToken, ^{ |
| 133 | + _dateFormatter = [[NSDateFormatter alloc] init]; |
| 134 | + _dateFormatter.dateStyle = NSDateFormatterMediumStyle; |
| 135 | + _dateFormatter.timeStyle = NSDateFormatterMediumStyle; |
| 136 | + }); |
| 137 | + |
| 138 | + return _dateFormatter; |
| 139 | +} |
| 140 | + |
| 141 | +- (NSString *)stringFromTimeInterval:(NSTimeInterval)timeInterval |
| 142 | +{ |
| 143 | + NSInteger roundedHours = floor(timeInterval / 3600); |
| 144 | + NSInteger roundedMinutes = floor((timeInterval - (3600 * roundedHours)) / 60); |
| 145 | + NSInteger roundedSeconds = round(timeInterval - (roundedHours * 60 * 60) - (roundedMinutes * 60)); |
| 146 | + |
| 147 | + if (roundedHours > 0) |
| 148 | + return [NSString stringWithFormat:@"%ld:%02ld:%02ld", (long)roundedHours, (long)roundedMinutes, (long)roundedSeconds]; |
| 149 | + |
| 150 | + else |
| 151 | + return [NSString stringWithFormat:@"%ld:%02ld", (long)roundedMinutes, (long)roundedSeconds]; |
| 152 | +} |
| 153 | + |
| 154 | +- (void)setImage:(UIImage *)image |
76 | 155 | { |
77 | | - [self setImage:image animated:YES withAccessibilityLabel:accessibilityLabel]; |
| 156 | + [self setImage:image animated:YES]; |
78 | 157 | } |
79 | 158 |
|
80 | | -- (void)setImage:(UIImage *)image animated:(BOOL)animated withAccessibilityLabel:(NSString*)accessibilityLabel |
| 159 | +- (void)setImage:(UIImage *)image animated:(BOOL)animated |
81 | 160 | { |
82 | 161 | if (!image){ |
83 | 162 | self.imageView.alpha = 0; |
84 | 163 | self.imageView.image = nil; |
85 | | - self.imageView.accessibilityLabel = nil; |
86 | 164 | } else { |
87 | 165 | if (animated) { |
88 | | - [UIView animateWithDuration:0.3 animations:^{ |
| 166 | + [UIView animateWithDuration:TimeForFadeAnimation animations:^{ |
89 | 167 | self.imageView.alpha = 1.0; |
90 | 168 | self.imageView.image = image; |
91 | | - self.imageView.accessibilityLabel = accessibilityLabel; |
92 | 169 | }]; |
93 | 170 | } else { |
94 | 171 | self.imageView.alpha = 1.0; |
95 | 172 | self.imageView.image = image; |
96 | | - self.imageView.accessibilityLabel = accessibilityLabel; |
97 | 173 | } |
98 | 174 | } |
99 | 175 | } |
|
0 commit comments