|
| 1 | +// |
| 2 | +// ASPhotosImageRequest.m |
| 3 | +// AsyncDisplayKit |
| 4 | +// |
| 5 | +// Created by Adlai Holler on 9/25/15. |
| 6 | +// Copyright © 2015 Facebook. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "ASPhotosImageRequest.h" |
| 10 | +#import "ASBaseDefines.h" |
| 11 | + |
| 12 | +NSString *const ASPhotosURLScheme = @"ph"; |
| 13 | + |
| 14 | +static NSString *const _ASPhotosURLQueryKeyWidth = @"width"; |
| 15 | +static NSString *const _ASPhotosURLQueryKeyHeight = @"height"; |
| 16 | + |
| 17 | +// value is PHImageContentMode value |
| 18 | +static NSString *const _ASPhotosURLQueryKeyContentMode = @"contentmode"; |
| 19 | + |
| 20 | +// value is PHImageRequestOptionsResizeMode value |
| 21 | +static NSString *const _ASPhotosURLQueryKeyResizeMode = @"resizemode"; |
| 22 | + |
| 23 | +// value is PHImageRequestOptionsVersion value |
| 24 | +static NSString *const _ASPhotosURLQueryKeyVersion = @"version"; |
| 25 | + |
| 26 | +// value is 0 or 1 |
| 27 | +static NSString *const _ASPhotosURLQueryKeyAllowNetworkAccess = @"network"; |
| 28 | + |
| 29 | +static NSString *const _ASPhotosURLQueryKeyCropOriginX = @"crop_x"; |
| 30 | +static NSString *const _ASPhotosURLQueryKeyCropOriginY = @"crop_y"; |
| 31 | +static NSString *const _ASPhotosURLQueryKeyCropWidth = @"crop_w"; |
| 32 | +static NSString *const _ASPhotosURLQueryKeyCropHeight = @"crop_h"; |
| 33 | + |
| 34 | +@implementation ASPhotosImageRequest |
| 35 | + |
| 36 | +- (instancetype)init |
| 37 | +{ |
| 38 | + ASDISPLAYNODE_NOT_DESIGNATED_INITIALIZER(); |
| 39 | + self = [self initWithAssetIdentifier:@""]; |
| 40 | + return nil; |
| 41 | +} |
| 42 | + |
| 43 | +- (instancetype)initWithAssetIdentifier:(NSString *)assetIdentifier |
| 44 | +{ |
| 45 | + self = [super init]; |
| 46 | + if (self) { |
| 47 | + _assetIdentifier = assetIdentifier; |
| 48 | + _options = [PHImageRequestOptions new]; |
| 49 | + _contentMode = PHImageContentModeDefault; |
| 50 | + _targetSize = PHImageManagerMaximumSize; |
| 51 | + } |
| 52 | + return self; |
| 53 | +} |
| 54 | + |
| 55 | +#pragma mark NSCopying |
| 56 | + |
| 57 | +- (id)copyWithZone:(NSZone *)zone |
| 58 | +{ |
| 59 | + ASPhotosImageRequest *copy = [[ASPhotosImageRequest alloc] initWithAssetIdentifier:self.assetIdentifier]; |
| 60 | + copy.options = [self.options copy]; |
| 61 | + copy.targetSize = self.targetSize; |
| 62 | + copy.contentMode = self.contentMode; |
| 63 | + return copy; |
| 64 | +} |
| 65 | + |
| 66 | +#pragma mark Converting to URL |
| 67 | + |
| 68 | +- (NSURL *)url |
| 69 | +{ |
| 70 | + NSURLComponents *comp = [NSURLComponents new]; |
| 71 | + comp.scheme = ASPhotosURLScheme; |
| 72 | + comp.host = _assetIdentifier; |
| 73 | + NSMutableArray *queryItems = [NSMutableArray arrayWithObjects: |
| 74 | + [NSURLQueryItem queryItemWithName:_ASPhotosURLQueryKeyWidth value:@(_targetSize.width).stringValue], |
| 75 | + [NSURLQueryItem queryItemWithName:_ASPhotosURLQueryKeyHeight value:@(_targetSize.height).stringValue], |
| 76 | + [NSURLQueryItem queryItemWithName:_ASPhotosURLQueryKeyVersion value:@(_options.version).stringValue], |
| 77 | + [NSURLQueryItem queryItemWithName:_ASPhotosURLQueryKeyContentMode value:@(_contentMode).stringValue], |
| 78 | + [NSURLQueryItem queryItemWithName:_ASPhotosURLQueryKeyAllowNetworkAccess value:@(_options.networkAccessAllowed).stringValue], |
| 79 | + [NSURLQueryItem queryItemWithName:_ASPhotosURLQueryKeyResizeMode value:@(_options.resizeMode).stringValue] |
| 80 | + , nil]; |
| 81 | + |
| 82 | + CGRect cropRect = _options.normalizedCropRect; |
| 83 | + if (!CGRectIsEmpty(cropRect)) { |
| 84 | + [queryItems addObjectsFromArray:@[ |
| 85 | + [NSURLQueryItem queryItemWithName:_ASPhotosURLQueryKeyCropOriginX value:@(cropRect.origin.x).stringValue], |
| 86 | + [NSURLQueryItem queryItemWithName:_ASPhotosURLQueryKeyCropOriginY value:@(cropRect.origin.y).stringValue], |
| 87 | + [NSURLQueryItem queryItemWithName:_ASPhotosURLQueryKeyCropWidth value:@(cropRect.size.width).stringValue], |
| 88 | + [NSURLQueryItem queryItemWithName:_ASPhotosURLQueryKeyCropHeight value:@(cropRect.size.height).stringValue] |
| 89 | + ]]; |
| 90 | + } |
| 91 | + comp.queryItems = queryItems; |
| 92 | + return comp.URL; |
| 93 | +} |
| 94 | + |
| 95 | +#pragma mark Converting from URL |
| 96 | + |
| 97 | ++ (ASPhotosImageRequest *)requestWithURL:(NSURL *)url |
| 98 | +{ |
| 99 | + NSURLComponents *comp = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO]; |
| 100 | + // not a photos URL |
| 101 | + if (![comp.scheme isEqualToString:ASPhotosURLScheme]) { |
| 102 | + return nil; |
| 103 | + } |
| 104 | + |
| 105 | + ASPhotosImageRequest *request = [[ASPhotosImageRequest alloc] initWithAssetIdentifier:url.host]; |
| 106 | + |
| 107 | + CGRect cropRect = CGRectZero; |
| 108 | + CGSize targetSize = PHImageManagerMaximumSize; |
| 109 | + for (NSURLQueryItem *item in comp.queryItems) { |
| 110 | + if ([_ASPhotosURLQueryKeyAllowNetworkAccess isEqualToString:item.name]) { |
| 111 | + request.options.networkAccessAllowed = item.value.boolValue; |
| 112 | + } else if ([_ASPhotosURLQueryKeyWidth isEqualToString:item.name]) { |
| 113 | + targetSize.width = item.value.doubleValue; |
| 114 | + } else if ([_ASPhotosURLQueryKeyHeight isEqualToString:item.name]) { |
| 115 | + targetSize.height = item.value.doubleValue; |
| 116 | + } else if ([_ASPhotosURLQueryKeyContentMode isEqualToString:item.name]) { |
| 117 | + request.contentMode = (PHImageContentMode)item.value.integerValue; |
| 118 | + } else if ([_ASPhotosURLQueryKeyVersion isEqualToString:item.name]) { |
| 119 | + request.options.version = (PHImageRequestOptionsVersion)item.value.integerValue; |
| 120 | + } else if ([_ASPhotosURLQueryKeyCropOriginX isEqualToString:item.name]) { |
| 121 | + cropRect.origin.x = item.value.doubleValue; |
| 122 | + } else if ([_ASPhotosURLQueryKeyCropOriginY isEqualToString:item.name]) { |
| 123 | + cropRect.origin.y = item.value.doubleValue; |
| 124 | + } else if ([_ASPhotosURLQueryKeyCropWidth isEqualToString:item.name]) { |
| 125 | + cropRect.size.width = item.value.doubleValue; |
| 126 | + } else if ([_ASPhotosURLQueryKeyCropHeight isEqualToString:item.name]) { |
| 127 | + cropRect.size.height = item.value.doubleValue; |
| 128 | + } else if ([_ASPhotosURLQueryKeyResizeMode isEqualToString:item.name]) { |
| 129 | + request.options.resizeMode = (PHImageRequestOptionsResizeMode)item.value.integerValue; |
| 130 | + } |
| 131 | + } |
| 132 | + request.targetSize = targetSize; |
| 133 | + request.options.normalizedCropRect = cropRect; |
| 134 | + return request; |
| 135 | +} |
| 136 | + |
| 137 | +#pragma mark NSObject |
| 138 | + |
| 139 | +- (BOOL)isEqual:(id)object |
| 140 | +{ |
| 141 | + if (![object isKindOfClass:ASPhotosImageRequest.class]) { |
| 142 | + return NO; |
| 143 | + } |
| 144 | + ASPhotosImageRequest *other = object; |
| 145 | + return [other.assetIdentifier isEqualToString:self.assetIdentifier] && |
| 146 | + other.contentMode == self.contentMode && |
| 147 | + CGSizeEqualToSize(other.targetSize, self.targetSize) && |
| 148 | + CGRectEqualToRect(other.options.normalizedCropRect, self.options.normalizedCropRect) && |
| 149 | + other.options.resizeMode == self.options.resizeMode && |
| 150 | + other.options.version == self.options.version; |
| 151 | +} |
| 152 | + |
| 153 | +@end |
| 154 | + |
| 155 | +@implementation NSURL (ASPhotosRequestConverting) |
| 156 | + |
| 157 | +- (ASPhotosImageRequest *)asyncdisplaykit_photosRequest |
| 158 | +{ |
| 159 | + return [ASPhotosImageRequest requestWithURL:self]; |
| 160 | +} |
| 161 | + |
| 162 | +@end |
0 commit comments