Search code examples
objective-cvideophotokit

how can I upload video use photokit?


before iOS8, I use ALAsset and use buffer to get NSData then upload video like below;

Byte * buffer = (Byte *)malloc(lenght);

NSError * error = nil;

NSUInteger readLength = [self.asset.defaultRepresentation getBytes:buffer fromOffset:self.sendedSize length:lenght error:&error];

NSData * data = [NSData dataWithBytes:buffer length:readLength];

so, how can i upload video use PhotoKit? How can i get the video data?


Solution

  • How about this:

        PHAsset *videoAsset /*Your video asset */;
        PHVideoRequestOptions *options = /*Options if you need them */;
        [[PHImageManager defaultManager] requestAVAssetForVideo:videoAsset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
            AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];
            NSURL *fileURL = /*Create a temporary file URL*/;
            exportSession.outputURL = fileURL;
    
            [exportSession exportAsynchronouslyWithCompletionHandler:^{
                NSData *assetData = [NSData dataWithContentsOfURL:fileURL];
                /* Do whatever you want to do with this data, and delete it afterwards */      
            }];
        }];