Skip to content
This repository was archived by the owner on Jun 19, 2025. It is now read-only.

Commit 270ab13

Browse files
committed
Merge branch 'release/0.7.2'
2 parents 4cee383 + 94db3aa commit 270ab13

File tree

5 files changed

+91
-88
lines changed

5 files changed

+91
-88
lines changed

Example/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PODS:
2-
- WPMediaPicker (0.7.1)
2+
- WPMediaPicker (0.7.2)
33

44
DEPENDENCIES:
55
- WPMediaPicker (from `../`)
@@ -9,6 +9,6 @@ EXTERNAL SOURCES:
99
:path: ../
1010

1111
SPEC CHECKSUMS:
12-
WPMediaPicker: d3d46adfbc9f34a4350f0f2defdafb04f32701f2
12+
WPMediaPicker: 22beabf079a54ece002208efd934e472eaedeed4
1313

1414
COCOAPODS: 0.39.0
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
@import UIKit;
2+
#import "WPMediaCollectionDataSource.h"
23

34
@interface WPMediaCollectionViewCell : UICollectionViewCell
45

5-
@property (nonatomic, strong) UIImage *image;
6+
@property (nonatomic, strong) id<WPMediaAsset> asset;
67
@property (nonatomic, assign) NSInteger position;
78

8-
- (void)setCaption:(NSString *)caption;
9-
10-
- (void)setImage:(UIImage *)image animated:(BOOL)animated withAccessibilityLabel:(NSString*)accessibilityLabel;
11-
129
@end

Pod/Classes/WPMediaCollectionViewCell.m

Lines changed: 84 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#import "WPMediaCollectionViewCell.h"
22

3+
static const NSTimeInterval ThredsholdForAnimation = 0.03;
4+
static const CGFloat TimeForFadeAnimation = 0.3;
5+
36
@interface WPMediaCollectionViewCell ()
47

58
@property (nonatomic, strong) UILabel *positionLabel;
@@ -32,7 +35,11 @@ - (id)initWithCoder:(NSCoder *)aDecoder
3235
- (void)prepareForReuse
3336
{
3437
[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];
3643
[self setCaption:@""];
3744
[self setPosition:NSNotFound];
3845
[self setSelected:NO];
@@ -72,28 +79,97 @@ - (void)commonInit
7279
[self.contentView addSubview:_captionLabel];
7380
}
7481

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
76155
{
77-
[self setImage:image animated:YES withAccessibilityLabel:accessibilityLabel];
156+
[self setImage:image animated:YES];
78157
}
79158

80-
- (void)setImage:(UIImage *)image animated:(BOOL)animated withAccessibilityLabel:(NSString*)accessibilityLabel
159+
- (void)setImage:(UIImage *)image animated:(BOOL)animated
81160
{
82161
if (!image){
83162
self.imageView.alpha = 0;
84163
self.imageView.image = nil;
85-
self.imageView.accessibilityLabel = nil;
86164
} else {
87165
if (animated) {
88-
[UIView animateWithDuration:0.3 animations:^{
166+
[UIView animateWithDuration:TimeForFadeAnimation animations:^{
89167
self.imageView.alpha = 1.0;
90168
self.imageView.image = image;
91-
self.imageView.accessibilityLabel = accessibilityLabel;
92169
}];
93170
} else {
94171
self.imageView.alpha = 1.0;
95172
self.imageView.image = image;
96-
self.imageView.accessibilityLabel = accessibilityLabel;
97173
}
98174
}
99175
}

Pod/Classes/WPMediaCollectionViewController.m

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -380,24 +380,6 @@ - (BOOL)isCaptureCellIndexPath:(NSIndexPath *)indexPath
380380
return asset;
381381
}
382382

383-
- (NSString *)accessibilityLabelForType:(WPMediaType)assetType atIndexPath:(NSInteger)index
384-
{
385-
NSString *accessibilityLabelFormat;
386-
switch (assetType) {
387-
case WPMediaTypeImage:
388-
accessibilityLabelFormat = NSLocalizedString(@"Asset %d, image.", @"Accessibility label for image thumbnails in the media collection view. The parameter is the index of the image in the collection view.");
389-
break;
390-
case WPMediaTypeVideo:
391-
accessibilityLabelFormat = NSLocalizedString(@"Asset %d, video", @"Accessibility label for video thumbnails in the media collection view. The parameter is the index of the video in the collection view.");
392-
break;
393-
default:
394-
accessibilityLabelFormat = NSLocalizedString(@"Asset %d", @"Accessibility label for asset (of unknown type) thumbnails in the media collection view. The parameter is the index of the asset in the collection view.");
395-
break;
396-
}
397-
398-
return [NSString stringWithFormat:accessibilityLabelFormat, index];
399-
}
400-
401383
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
402384
{
403385
if ([self isCaptureCellIndexPath:indexPath]) {
@@ -408,40 +390,9 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
408390

409391
id<WPMediaAsset> asset = [self assetForPosition:indexPath];
410392
WPMediaCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([WPMediaCollectionViewCell class]) forIndexPath:indexPath];
411-
if (cell.tag != 0) {
412-
[asset cancelImageRequest:(WPMediaRequestID)cell.tag];
413-
cell.tag = 0;
414-
}
393+
415394
// Configure the cell
416-
__block WPMediaRequestID requestKey = 0;
417-
NSTimeInterval timestamp = [NSDate timeIntervalSinceReferenceDate];
418-
NSInteger itemIndex = indexPath.item + 1; // Asset position + 1 to avoid starting at "Asset 0"
419-
WPMediaType assetType = asset.assetType;
420-
requestKey = [asset imageWithSize:cell.frame.size completionHandler:^(UIImage *result, NSError *error) {
421-
BOOL animated = ([NSDate timeIntervalSinceReferenceDate] - timestamp) > 0.03;
422-
if (error) {
423-
cell.image = nil;
424-
NSLog(@"%@", [error localizedDescription]);
425-
return;
426-
}
427-
void (^setImage)() = ^{
428-
if (requestKey == cell.tag){
429-
NSString *accessibilityLabel = [self accessibilityLabelForType:assetType
430-
atIndexPath:itemIndex];
431-
[cell setImage:result
432-
animated:animated
433-
withAccessibilityLabel:accessibilityLabel];
434-
}
435-
};
436-
if ([NSThread isMainThread]){
437-
setImage();
438-
} else {
439-
dispatch_async(dispatch_get_main_queue(), ^{
440-
setImage();
441-
});
442-
}
443-
}];
444-
cell.tag = requestKey;
395+
cell.asset = asset;
445396
NSUInteger position = [self positionOfAssetInSelection:asset];
446397
if (position != NSNotFound) {
447398
[self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
@@ -456,30 +407,9 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
456407
cell.selected = NO;
457408
}
458409

459-
if ([asset assetType] == WPMediaTypeVideo) {
460-
NSTimeInterval duration = [asset duration];
461-
NSString *caption = [self stringFromTimeInterval:duration];
462-
[cell setCaption:caption];
463-
} else {
464-
[cell setCaption:@""];
465-
}
466-
467410
return cell;
468411
}
469412

470-
- (NSString *)stringFromTimeInterval:(NSTimeInterval)timeInterval
471-
{
472-
NSInteger roundedHours = floor(timeInterval / 3600);
473-
NSInteger roundedMinutes = floor((timeInterval - (3600 * roundedHours)) / 60);
474-
NSInteger roundedSeconds = round(timeInterval - (roundedHours * 60 * 60) - (roundedMinutes * 60));
475-
476-
if (roundedHours > 0)
477-
return [NSString stringWithFormat:@"%ld:%02ld:%02ld", (long)roundedHours, (long)roundedMinutes, (long)roundedSeconds];
478-
479-
else
480-
return [NSString stringWithFormat:@"%ld:%02ld", (long)roundedMinutes, (long)roundedSeconds];
481-
}
482-
483413
- (NSUInteger)positionOfAssetInSelection:(id<WPMediaAsset>)asset
484414
{
485415
NSUInteger position = [self.selectedAssets indexOfObjectPassingTest:^BOOL(id<WPMediaAsset> loopAsset, NSUInteger idx, BOOL *stop) {

WPMediaPicker.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "WPMediaPicker"
3-
s.version = "0.7.1"
3+
s.version = "0.7.2"
44
s.summary = "WPMediaPicker is an iOS controller that allows capture and picking of media assets."
55
s.description = <<-DESC
66
WPMediaPicker is an iOS controller that allows capture and picking of media assets.

0 commit comments

Comments
 (0)