Skip to content

Instantly share code, notes, and snippets.

@kmdarshan
Created April 8, 2019 22:56
Show Gist options
  • Save kmdarshan/b56c06d8a432a3639911f9adc609aed4 to your computer and use it in GitHub Desktop.
Save kmdarshan/b56c06d8a432a3639911f9adc609aed4 to your computer and use it in GitHub Desktop.
Exporting media in iOS
// First thing is you need a AVAsset.
// Once you have an asset, you need to determine the type of preset you need to use.
// You can consider a preset as a set of properties, which is provided to the exporter.
// I do this for helping me with my export.
-(NSString*)determineCompatibleExportPresetForAsset:(AVAsset*)asset
{
NSArray<NSString*>* presetsRankedByPriority = @[AVAssetExportPresetHighestQuality, AVAssetExportPresetMediumQuality, AVAssetExportPresetLowQuality];
NSArray* compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset];
for (NSString* preset in presetsRankedByPriority)
{
if([compatiblePresets containsObject:preset])
{
return preset;
}
}
return nil;
}
// after getting the preset, use this call
AVAssetExportSession* exportSession = [AVAssetExportSession exportSessionWithAsset:asset presetName:preset];
exportSession.outputFileType = UTI;
exportSession.outputURL = whereToSaveTheFile;
if (!exportSession)
{
callback([NSError errorWithDomain:kManagerErrorDomain
code:kDownloadFailed
userInfo:nil]);
}
else
{
[exportSession exportAsynchronouslyWithCompletionHandler: ^{
// handle error here
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment