Skip to content

Commit fe25673

Browse files
authored
Merge pull request #10 from SDWebImage/feature_request_video_type_asset
Add option to request other type of PHAsset like Video during loading
2 parents 2cc4ff3 + 34d5b69 commit fe25673

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

Example/SDWebImagePhotosPlugin/MasterViewController.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibB
5959
PHImageRequestOptions *options = [PHImageRequestOptions new];
6060
options.sd_targetSize = CGSizeMake(500, 500); // The original image size may be 4K, we only query the max view size :)
6161
SDWebImagePhotosLoader.sharedLoader.imageRequestOptions = options;
62+
// Request Video Asset Poster as well
63+
SDWebImagePhotosLoader.sharedLoader.requestImageAssetOnly = NO;
6264

6365
// Photos Library Demo
6466
[self fetchAssets];
@@ -73,7 +75,6 @@ - (void)fetchAssets {
7375
options:nil];
7476
PHAssetCollection *collection = result.firstObject;
7577
PHFetchOptions *fetchOptions = [PHFetchOptions new];
76-
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d", PHAssetMediaTypeImage];
7778
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
7879
PHFetchResult<PHAsset *> *assets = [PHAsset fetchAssetsInAssetCollection:collection options:fetchOptions];
7980
for (PHAsset *asset in assets) {

SDWebImagePhotosPlugin/Classes/SDWebImagePhotosLoader.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,11 @@
3737
*/
3838
@property (nonatomic, strong, nullable) PHImageRequestOptions *imageRequestOptions;
3939

40+
/**
41+
Whether we request only `PHAssetMediaTypeImage` asset and ignore other types (video/audio/unknown).
42+
When we found other type, an error `SDWebImagePhotosErrorNotImageAsset` will be reported.
43+
Defaults to YES. If you prefer to load other type like video asset's poster image, set this value to NO.
44+
*/
45+
@property (nonatomic, assign) BOOL requestImageAssetOnly;
46+
4047
@end

SDWebImagePhotosPlugin/Classes/SDWebImagePhotosLoader.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ - (instancetype)init {
7777
requestOptions.version = PHImageRequestOptionsVersionCurrent;
7878
self.imageRequestOptions = requestOptions;
7979
self.operationsTable = [NSHashTable weakObjectsHashTable];
80+
self.requestImageAssetOnly = YES;
8081
self.fetchQueue = dispatch_queue_create("SDWebImagePhotosLoader", DISPATCH_QUEUE_SERIAL);
8182
#if SD_UIKIT
8283
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
@@ -155,8 +156,8 @@ - (BOOL)canRequestImageForURL:(NSURL *)url {
155156
PHFetchResult<PHAsset *> *fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[localIdentifier] options:fetchOptions];
156157
asset = fetchResult.firstObject;
157158
}
158-
// Only support image
159-
if (!asset || asset.mediaType != PHAssetMediaTypeImage) {
159+
// Check whether we should request only image asset
160+
if (!asset || (self.requestImageAssetOnly && asset.mediaType != PHAssetMediaTypeImage)) {
160161
// Call error
161162
NSError *error = [NSError errorWithDomain:SDWebImagePhotosErrorDomain code:SDWebImagePhotosErrorNotImageAsset userInfo:nil];
162163
if (completedBlock) {

0 commit comments

Comments
 (0)