Skip to content

Commit c56d532

Browse files
authored
Merge pull request #21 from SDWebImage/feature_convenience_API
Added the convenience API to get URL representation from PHAsset object
2 parents eaa2393 + 58fdf4c commit c56d532

File tree

8 files changed

+63
-44
lines changed

8 files changed

+63
-44
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Carthage/Build
3434
# `pod install` in .travis.yml
3535
#
3636
Example/Pods
37+
Example/Podfile.lock
3738

3839
# SwiftPM
3940
.swiftpm

Example/Podfile.lock

Lines changed: 0 additions & 27 deletions
This file was deleted.

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ To start loading the Photos Library image, use the `NSURL+SDWebImagePhotosPlugin
103103
```objectivec
104104
// Create with `PHAsset`
105105
PHAsset *asset;
106-
NSURL *photosURL = [NSURL sd_URLWithAsset:asset];
106+
NSURL *photosURL = asset.sd_URLRepresentation;
107+
// The same as `[NSURL sd_URLWithAsset:asset];`
107108
// Create with `localIdentifier`
108109
NSString *identifier;
109110
NSURL *potosURL = [NSURL sd_URLWithAssetLocalIdentifier:identifier];
@@ -117,10 +118,11 @@ NSURL *potosURL = [NSURL sd_URLWithAssetLocalIdentifier:identifier];
117118
```swift
118119
// Create with `PHAsset`
119120
let asset: PHAsset
120-
let photosURL = NSURL.sd_URL(with: asset)
121+
let photosURL = asset.sd_URLRepresentation
122+
// The same as `NSURL.sd_URL(with: asset) as URL`
121123
// Create with `localIdentifier`
122124
let identifier: String
123-
let potosURL = NSURL.sd_URL(withAssetLocalIdentifier: identifier)
125+
let potosURL = NSURL.sd_URL(withAssetLocalIdentifier: identifier) as URL
124126
125127
// Load image (assume using custom manager)
126128
imageView.sd_setImage(with: photosURL, placeholderImage: nil, context: [.customManager: manager])
@@ -210,7 +212,7 @@ Control query image size for individual assets:
210212
```objective-c
211213
UIImageView *imageView;
212214
PHAsset *asset;
213-
NSURL *url = [NSURL sd_URLWithAsset:asset];
215+
NSURL *url = asset.sd_URLRepresentation;
214216
[imageView.sd_setImageWithURL:url options:0 context:@{SDWebImageContextImageThumbnailPixelSize: @(imageView.bounds.size)}]; // Fetch image based on image view size
215217
```
216218
@@ -219,7 +221,7 @@ NSURL *url = [NSURL sd_URLWithAsset:asset];
219221
```swift
220222
let imageView: UIImageView
221223
let asset: PHAsset
222-
let url = URL.sd_URL(with: asset)
224+
let url = asset.sd_URLRepresentation
223225
imageView.sd_setImage(with: url, context: [.imageThumbnailPixelSize : imageView.bounds.size]) // Fetch image based on image view size
224226
```
225227

SDWebImagePhotosPlugin.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
3235B28021FAD1E6008668C5 /* Photos.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3235B27F21FAD1E6008668C5 /* Photos.framework */; };
1111
3235B28221FAD1ED008668C5 /* Photos.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3235B28121FAD1ED008668C5 /* Photos.framework */; };
1212
3235B28421FAD1F5008668C5 /* Photos.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3235B28321FAD1F5008668C5 /* Photos.framework */; };
13+
32380B5425B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.h in Headers */ = {isa = PBXBuildFile; fileRef = 32380B5225B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.h */; settings = {ATTRIBUTES = (Public, ); }; };
14+
32380B5525B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.h in Headers */ = {isa = PBXBuildFile; fileRef = 32380B5225B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.h */; settings = {ATTRIBUTES = (Public, ); }; };
15+
32380B5625B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.h in Headers */ = {isa = PBXBuildFile; fileRef = 32380B5225B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.h */; settings = {ATTRIBUTES = (Public, ); }; };
16+
32380B5725B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.m in Sources */ = {isa = PBXBuildFile; fileRef = 32380B5325B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.m */; };
17+
32380B5825B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.m in Sources */ = {isa = PBXBuildFile; fileRef = 32380B5325B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.m */; };
18+
32380B5925B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.m in Sources */ = {isa = PBXBuildFile; fileRef = 32380B5325B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.m */; };
1319
32CCEA13213E38CD00214381 /* SDWebImagePhotosPlugin.h in Headers */ = {isa = PBXBuildFile; fileRef = 32CCEA12213E38CD00214381 /* SDWebImagePhotosPlugin.h */; settings = {ATTRIBUTES = (Public, ); }; };
1420
32CCEA14213E38CD00214381 /* SDWebImagePhotosPlugin.h in Headers */ = {isa = PBXBuildFile; fileRef = 32CCEA12213E38CD00214381 /* SDWebImagePhotosPlugin.h */; settings = {ATTRIBUTES = (Public, ); }; };
1521
32CCEA15213E38CD00214381 /* SDWebImagePhotosPlugin.h in Headers */ = {isa = PBXBuildFile; fileRef = 32CCEA12213E38CD00214381 /* SDWebImagePhotosPlugin.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -52,6 +58,8 @@
5258
3235B27F21FAD1E6008668C5 /* Photos.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Photos.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Photos.framework; sourceTree = DEVELOPER_DIR; };
5359
3235B28121FAD1ED008668C5 /* Photos.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Photos.framework; path = Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS12.1.sdk/System/Library/Frameworks/Photos.framework; sourceTree = DEVELOPER_DIR; };
5460
3235B28321FAD1F5008668C5 /* Photos.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Photos.framework; path = System/Library/Frameworks/Photos.framework; sourceTree = SDKROOT; };
61+
32380B5225B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "PHAsset+SDWebImagePhotosPlugin.h"; sourceTree = "<group>"; };
62+
32380B5325B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "PHAsset+SDWebImagePhotosPlugin.m"; sourceTree = "<group>"; };
5563
32CCE9E8213E383E00214381 /* SDWebImagePhotosPlugin.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SDWebImagePhotosPlugin.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5664
32CCE9F8213E384500214381 /* SDWebImagePhotosPlugin_tvOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SDWebImagePhotosPlugin_tvOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5765
32CCEA05213E385A00214381 /* SDWebImagePhotosPlugin_macOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SDWebImagePhotosPlugin_macOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -145,6 +153,8 @@
145153
32CCEA1D213E38F600214381 /* SDWebImagePhotosError.m */,
146154
32CCEA20213E38F600214381 /* SDImagePhotosLoader.h */,
147155
32CCEA1B213E38F600214381 /* SDImagePhotosLoader.m */,
156+
32380B5225B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.h */,
157+
32380B5325B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.m */,
148158
);
149159
path = Classes;
150160
sourceTree = "<group>";
@@ -170,6 +180,7 @@
170180
buildActionMask = 2147483647;
171181
files = (
172182
32CCEA28213E38F600214381 /* SDWebImagePhotosDefine.h in Headers */,
183+
32380B5425B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.h in Headers */,
173184
32CCEA31213E38F600214381 /* PHImageRequestOptions+SDWebImagePhotosPlugin.h in Headers */,
174185
32CCEA40213E38F600214381 /* SDWebImagePhotosError.h in Headers */,
175186
32CCEA34213E38F600214381 /* SDImagePhotosLoader.h in Headers */,
@@ -183,6 +194,7 @@
183194
buildActionMask = 2147483647;
184195
files = (
185196
32CCEA29213E38F600214381 /* SDWebImagePhotosDefine.h in Headers */,
197+
32380B5525B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.h in Headers */,
186198
32CCEA32213E38F600214381 /* PHImageRequestOptions+SDWebImagePhotosPlugin.h in Headers */,
187199
32CCEA41213E38F600214381 /* SDWebImagePhotosError.h in Headers */,
188200
32CCEA35213E38F600214381 /* SDImagePhotosLoader.h in Headers */,
@@ -196,6 +208,7 @@
196208
buildActionMask = 2147483647;
197209
files = (
198210
32CCEA2A213E38F600214381 /* SDWebImagePhotosDefine.h in Headers */,
211+
32380B5625B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.h in Headers */,
199212
32CCEA33213E38F600214381 /* PHImageRequestOptions+SDWebImagePhotosPlugin.h in Headers */,
200213
32CCEA42213E38F600214381 /* SDWebImagePhotosError.h in Headers */,
201214
32CCEA36213E38F600214381 /* SDImagePhotosLoader.h in Headers */,
@@ -333,6 +346,7 @@
333346
32CCEA3A213E38F600214381 /* PHImageRequestOptions+SDWebImagePhotosPlugin.m in Sources */,
334347
32CCEA2B213E38F600214381 /* SDWebImagePhotosError.m in Sources */,
335348
32CCEA2E213E38F600214381 /* NSURL+SDWebImagePhotosPlugin.m in Sources */,
349+
32380B5725B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.m in Sources */,
336350
32CCEA37213E38F600214381 /* SDWebImagePhotosDefine.m in Sources */,
337351
);
338352
runOnlyForDeploymentPostprocessing = 0;
@@ -345,6 +359,7 @@
345359
32CCEA3B213E38F600214381 /* PHImageRequestOptions+SDWebImagePhotosPlugin.m in Sources */,
346360
32CCEA2C213E38F600214381 /* SDWebImagePhotosError.m in Sources */,
347361
32CCEA2F213E38F600214381 /* NSURL+SDWebImagePhotosPlugin.m in Sources */,
362+
32380B5825B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.m in Sources */,
348363
32CCEA38213E38F600214381 /* SDWebImagePhotosDefine.m in Sources */,
349364
);
350365
runOnlyForDeploymentPostprocessing = 0;
@@ -357,6 +372,7 @@
357372
32CCEA3C213E38F600214381 /* PHImageRequestOptions+SDWebImagePhotosPlugin.m in Sources */,
358373
32CCEA2D213E38F600214381 /* SDWebImagePhotosError.m in Sources */,
359374
32CCEA30213E38F600214381 /* NSURL+SDWebImagePhotosPlugin.m in Sources */,
375+
32380B5925B95E87002A50E1 /* PHAsset+SDWebImagePhotosPlugin.m in Sources */,
360376
32CCEA39213E38F600214381 /* SDWebImagePhotosDefine.m in Sources */,
361377
);
362378
runOnlyForDeploymentPostprocessing = 0;

SDWebImagePhotosPlugin/Classes/NSURL+SDWebImagePhotosPlugin.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@
3333
@param identifier `PHAsset` 's local identifier
3434
@return A Photos URL
3535
*/
36-
+ (nullable instancetype)sd_URLWithAssetLocalIdentifier:(nonnull NSString *)identifier;
36+
+ (nonnull instancetype)sd_URLWithAssetLocalIdentifier:(nonnull NSString *)identifier;
3737

3838
/**
3939
Create a Photos URL with `PHAsset`
4040
4141
@param asset `PHAsset` object
4242
@return A Photos URL
4343
*/
44-
+ (nullable instancetype)sd_URLWithAsset:(nonnull PHAsset *)asset;
44+
+ (nonnull instancetype)sd_URLWithAsset:(nonnull PHAsset *)asset;
4545

4646
@end

SDWebImagePhotosPlugin/Classes/NSURL+SDWebImagePhotosPlugin.m

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,15 @@ - (void)setSd_asset:(PHAsset * _Nullable)sd_asset {
4242
}
4343

4444
+ (instancetype)sd_URLWithAssetLocalIdentifier:(NSString *)identifier {
45-
if (!identifier) {
46-
return nil;
47-
}
45+
NSParameterAssert(identifier);
4846
// ph://F2A9F582-BA45-4308-924E-6D146B784A09/L0/001
4947
NSString *prefix = [NSString stringWithFormat:@"%@://", SDWebImagePhotosScheme];
5048
return [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", prefix, identifier]];
5149
}
5250

5351
+ (instancetype)sd_URLWithAsset:(PHAsset *)asset {
54-
if (![asset isKindOfClass:[PHAsset class]]) {
55-
return nil;
56-
}
52+
NSParameterAssert(asset);
5753
NSString *localIdentifier = asset.localIdentifier;
58-
if (!localIdentifier) {
59-
return nil;
60-
}
61-
6254
NSURL *url = [self sd_URLWithAssetLocalIdentifier:localIdentifier];
6355
url.sd_asset = asset;
6456

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* This file is part of the SDWebImage package.
3+
* (c) Olivier Poitrey <[email protected]>
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
#import <Photos/Photos.h>
10+
11+
@interface PHAsset (SDWebImagePhotosPlugin)
12+
13+
/// The convenience way to retrive the URL representation of PHAsset. The same as `+[NSURL sd_URLWithAsset:]`.
14+
/// @note This API always return new object in Objective-C/struct in Swift of URL.
15+
@property (nonatomic, strong, readonly, nonnull) NSURL *sd_URLRepresentation;
16+
17+
@end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* This file is part of the SDWebImage package.
3+
* (c) Olivier Poitrey <[email protected]>
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
#import "PHAsset+SDWebImagePhotosPlugin.h"
10+
#import "NSURL+SDWebImagePhotosPlugin.h"
11+
12+
@implementation PHAsset (SDWebImagePhotosPlugin)
13+
14+
- (NSURL *)sd_URLRepresentation {
15+
return [NSURL sd_URLWithAsset:self];
16+
}
17+
18+
@end

0 commit comments

Comments
 (0)