Skip to content

Commit 1383d9c

Browse files
committed
Update the demo for iOS && macOS
Refactory code and fix the layout issue on macOS 10.15. Use Command+R to reload assets on macOS
1 parent 83b245f commit 1383d9c

File tree

5 files changed

+64
-29
lines changed

5 files changed

+64
-29
lines changed

Example/SDWebImagePhotosPlugin/MasterViewController.m

+7-3
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,17 @@ - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibB
6161
SDWebImagePhotosLoader.sharedLoader.imageRequestOptions = options;
6262
// Request Video Asset Poster as well
6363
SDWebImagePhotosLoader.sharedLoader.requestImageAssetOnly = NO;
64-
65-
// Photos Library Demo
66-
[self fetchAssets];
6764
}
6865
return self;
6966
}
7067

68+
- (void)viewDidLoad {
69+
[super viewDidLoad];
70+
71+
// Photos Library Demo
72+
[self reloadData];
73+
}
74+
7175
- (void)fetchAssets {
7276
[self.objects removeAllObjects];
7377
PHFetchResult<PHAssetCollection *> *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum

Example/SDWebImagePhotosPlugin_Example macOS/AppDelegate.m

+3
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@ - (void)applicationWillTerminate:(NSNotification *)aNotification {
2323
// Insert code here to tear down your application
2424
}
2525

26+
- (IBAction)reloadButtonDidTap:(NSMenuItem *)sender {
27+
// Menu item target-action
28+
}
2629

2730
@end

Example/SDWebImagePhotosPlugin_Example macOS/Base.lproj/Main.storyboard

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="14868" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
33
<dependencies>
44
<deployment identifier="macosx"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14490.70"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14868"/>
66
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
77
</dependencies>
88
<scenes>
@@ -24,6 +24,11 @@
2424
</menuItem>
2525
<menuItem isSeparatorItem="YES" id="VOq-y0-SEH"/>
2626
<menuItem title="Preferences…" keyEquivalent="," id="BOF-NM-1cW"/>
27+
<menuItem title="Reload" keyEquivalent="r" id="D7H-QO-i7O">
28+
<connections>
29+
<action selector="reloadButtonDidTap:" target="Voe-Tx-rLC" id="IlR-kM-f1C"/>
30+
</connections>
31+
</menuItem>
2732
<menuItem isSeparatorItem="YES" id="wFC-TO-SCJ"/>
2833
<menuItem title="Services" id="NMo-om-nkz">
2934
<modifierMask key="keyEquivalentModifierMask"/>
@@ -719,11 +724,15 @@
719724
<collectionView id="Oe5-c8-Che">
720725
<rect key="frame" x="0.0" y="0.0" width="478" height="158"/>
721726
<autoresizingMask key="autoresizingMask" widthSizable="YES"/>
722-
<collectionViewGridLayout key="collectionViewLayout" maximumNumberOfRows="5" maximumNumberOfColumns="5" id="dpc-Ya-fwT">
727+
<collectionViewGridLayout key="collectionViewLayout" id="dpc-Ya-fwT">
723728
<size key="minimumItemSize" width="300" height="300"/>
724729
<size key="maximumItemSize" width="500" height="500"/>
725730
</collectionViewGridLayout>
726731
<color key="primaryBackgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
732+
<connections>
733+
<outlet property="dataSource" destination="XfG-lQ-9wD" id="hdW-D8-ACZ"/>
734+
<outlet property="delegate" destination="XfG-lQ-9wD" id="TAv-Dm-nUp"/>
735+
</connections>
727736
</collectionView>
728737
</subviews>
729738
</clipView>

Example/SDWebImagePhotosPlugin_Example macOS/TestCollectionViewItem.xib

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14868" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
33
<dependencies>
44
<deployment identifier="macosx"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14490.70"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14868"/>
66
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
77
</dependencies>
88
<objects>
99
<customObject id="-2" userLabel="File's Owner" customClass="TestCollectionViewItem">
1010
<connections>
11-
<outlet property="imageViewDisplay" destination="Ouc-ZV-Oxv" id="1bZ-DY-npt"/>
1211
<outlet property="view" destination="Hz6-mo-xeY" id="0bl-1N-x8E"/>
1312
</connections>
1413
</customObject>
@@ -24,6 +23,13 @@
2423
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" imageScaling="proportionallyDown" id="XMf-SK-k5R"/>
2524
</imageView>
2625
</subviews>
26+
<point key="canvasLocation" x="139" y="154"/>
2727
</customView>
28+
<collectionViewItem id="M6i-NW-lxd" customClass="TestCollectionViewItem">
29+
<connections>
30+
<outlet property="imageViewDisplay" destination="Ouc-ZV-Oxv" id="qRY-7I-fFV"/>
31+
<outlet property="view" destination="Hz6-mo-xeY" id="KRu-vJ-YAZ"/>
32+
</connections>
33+
</collectionViewItem>
2834
</objects>
2935
</document>

Example/SDWebImagePhotosPlugin_Example macOS/ViewController.m

+33-20
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
#import "TestCollectionViewItem.h"
1212
#import "PHCollection.h" // Currently seems `PHAssetCollection` is not list in public header, but it works
1313

14-
static NSString * const TestCollectionViewItemIdentifier = @"TestCollectionViewItemIdentifier";
15-
1614
@interface ViewController () <NSCollectionViewDelegate, NSCollectionViewDataSource>
1715

1816
@property (nonatomic, strong) NSMutableArray<NSURL *> *objects;
@@ -24,19 +22,33 @@ @interface ViewController () <NSCollectionViewDelegate, NSCollectionViewDataSour
2422

2523
@implementation ViewController
2624

25+
- (instancetype)initWithCoder:(NSCoder *)coder {
26+
self = [super initWithCoder:coder];
27+
if (self) {
28+
self.objects = [NSMutableArray array];
29+
// Setup Photos Loader
30+
SDWebImageManager.defaultImageLoader = [SDWebImagePhotosLoader sharedLoader];
31+
PHImageRequestOptions *options = [PHImageRequestOptions new];
32+
options.sd_targetSize = CGSizeMake(500, 500); // The original image size may be 4K, we only query the max view size :)
33+
SDWebImagePhotosLoader.sharedLoader.imageRequestOptions = options;
34+
35+
// Reload
36+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuItemDidTap:) name:NSMenuDidSendActionNotification object:nil];
37+
}
38+
return self;
39+
}
40+
2741
- (void)viewDidLoad {
2842
[super viewDidLoad];
2943

30-
self.objects = [NSMutableArray array];
31-
// Setup Photos Loader
32-
SDWebImageManager.defaultImageLoader = [SDWebImagePhotosLoader sharedLoader];
33-
PHImageRequestOptions *options = [PHImageRequestOptions new];
34-
options.sd_targetSize = CGSizeMake(500, 500); // The original image size may be 4K, we only query the max view size :)
35-
SDWebImagePhotosLoader.sharedLoader.imageRequestOptions = options;
36-
3744
// Photos Library Demo
38-
PHFetchResult<PHAssetCollection *> *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
39-
subtype:PHAssetCollectionSubtypeAlbumImported
45+
[self reloadData];
46+
}
47+
48+
- (void)fetchAssets {
49+
[self.objects removeAllObjects];
50+
PHFetchResult<PHAssetCollection *> *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum
51+
subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary
4052
options:nil];
4153
PHAssetCollection *collection = result.firstObject;
4254
PHFetchOptions *fetchOptions = [PHFetchOptions new];
@@ -50,21 +62,22 @@ - (void)viewDidLoad {
5062
NSURL *url = [NSURL sd_URLWithAsset:asset];
5163
[self.objects addObject:url];
5264
}
53-
54-
[self.collectionView registerClass:[TestCollectionViewItem class] forItemWithIdentifier:TestCollectionViewItemIdentifier];
55-
self.collectionView.delegate = self;
56-
self.collectionView.dataSource = self;
5765
}
5866

67+
- (void)menuItemDidTap:(NSNotification *)notification {
68+
NSMenuItem *menuItem = notification.userInfo[@"MenuItem"];
69+
if ([menuItem.title isEqualToString:@"Reload"]) {
70+
[self reloadData];
71+
}
72+
}
5973

60-
- (void)setRepresentedObject:(id)representedObject {
61-
[super setRepresentedObject:representedObject];
62-
63-
// Update the view, if already loaded.
74+
- (void)reloadData {
75+
[self fetchAssets];
76+
[self.collectionView reloadData];
6477
}
6578

6679
- (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath {
67-
TestCollectionViewItem *cell = [collectionView makeItemWithIdentifier:TestCollectionViewItemIdentifier forIndexPath:indexPath];
80+
TestCollectionViewItem *cell = [collectionView makeItemWithIdentifier:@"TestCollectionViewItem" forIndexPath:indexPath];
6881
NSURL *photosURL = self.objects[indexPath.item];
6982
cell.imageViewDisplay.sd_imageTransition = SDWebImageTransition.fadeTransition;
7083
[cell.imageViewDisplay sd_setImageWithURL:photosURL placeholderImage:nil options:SDWebImageFromLoaderOnly context:@{SDWebImageContextStoreCacheType: @(SDImageCacheTypeNone)}];

0 commit comments

Comments
 (0)