Created
August 12, 2016 07:52
-
-
Save pavelosipov/8add71e9ac5f08286ea9472f8f7df777 to your computer and use it in GitHub Desktop.
ALAsset from PHAsset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation PHImageManager (MRCApp) | |
+ (RACSignal *)p_fetchALAssetForVideoAsset:(PHAsset *)asset { | |
POSRX_CHECK(asset); | |
PHVideoRequestOptions *options = [PHVideoRequestOptions new]; | |
options.version = PHVideoRequestOptionsVersionCurrent; | |
options.deliveryMode = PHVideoRequestOptionsDeliveryModeHighQualityFormat; | |
options.networkAccessAllowed = YES; | |
return [self p_fetchALAssetForAsset:asset withURL:asset.mrc_videoAssetURL]; | |
} | |
+ (RACSignal *)p_fetchALAssetForAsset:(PHAsset *)asset withURL:(nullable NSURL *)assetURL { | |
if (!assetURL) { | |
return [RACSignal error:[NSError mrc_internalErrorWithFormat:@"Failed to build ALAsset URL for %@", asset]]; | |
} | |
ALAssetsLibrary *assetsLibrary = [ALAssetsLibrary new]; | |
return [[assetsLibrary mrc_fetchAssetForURL:assetURL] flattenMap:^RACSignal *(ALAsset *compatibleAsset) { | |
ALAssetRepresentation *assetRepresentation = compatibleAsset.defaultRepresentation; | |
if (!assetRepresentation) { | |
return [RACSignal error:[NSError mrc_internalErrorWithFormat: | |
@"ALAsset was not found for %@ with '%@'", asset, assetURL]]; | |
} | |
return [RACSignal return:RACTuplePack(compatibleAsset, assetsLibrary)]; | |
}]; | |
} | |
@end | |
#pragma mark - | |
@implementation PHAsset (MRCApp) | |
- (nullable NSURL *)mrc_videoAssetURL { | |
return [self p_directURLWithExtension:@"MOV"]; | |
} | |
- (nullable NSURL *)p_directURLWithExtension:(NSString *)extension { | |
NSString *assetID = [[self.localIdentifier componentsSeparatedByString:@"/"] firstObject]; | |
if (extension && assetID) { | |
return [NSURL URLWithString:[NSString stringWithFormat:@"assets-library://asset/asset.%@?id=%@&ext=%@", | |
extension, assetID, extension]]; | |
} | |
return nil; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment