简体   繁体   English

如何在iOS中找到照片库路径URL

[英]How to find the photo gallery path URL in iOS

I'm making an iOS app where the user will be able to record a video and as soon as he finish such record, he can send that video to a server. 我正在制作一个iOS应用程序,用户可以在其中录制视频,并且一旦完成录制,他就可以将该视频发送到服务器。 So far so good! 到现在为止还挺好! But to find the recorded video is being a headache! 但是要找到录制的视频真是令人头疼! How can I find the proper URL for the recorded video? 如何找到录制视频的正确URL? During my tests, I have saved a video in the Supporting Files folder and my code is the following 在测试期间,我已将视频保存在Supporting Files文件夹中,代码如下

 NSString *url = [[NSBundle mainBundle] pathForResource:@"trailer_iphone" ofType:@".m4v"];

 NSData *data = [NSData dataWithContentsOfFile:url];

So what could I do to replace that URL for the one in the photo gallery? 那么,我该怎么做才能替换照相馆中的那个URL?

In the Apple Developer web site is showing that we can use the AVAsset in order to access the photo gallery. 在Apple Developer网站上显示,我们可以使用AVAsset来访问照片库。 so I copied into my code and I'm trying to figure this out! 所以我将其复制到我的代码中,并试图找出答案!

//Copied from Apple website
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

    // Within the group enumeration block, filter to enumerate just videos.
    [group setAssetsFilter:[ALAssetsFilter allVideos]];

    // For this example, we're only interested in the first item.
    [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:0]
                            options:0
                         usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {

                             // The end of the enumeration is signaled by asset == nil.
                             if (alAsset) {
                                 ALAssetRepresentation *representation = [alAsset defaultRepresentation];
                                 NSURL *url = [representation url];
                                 AVAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
                                 // Do something interesting with the AV asset.



//My last code
//NSString *url = [[NSBundle mainBundle] pathForResource:@"trailer_iphone" ofType:@".m4v"];

NSData *data = [NSData dataWithContentsOfURL:url];

使用UISaveVideoAtPathToSavedPhotosAlbum 官方文档

For recording a video and send this recorded video file to server you can use AVFoundation framework as follow: AVFoundation Video Recording 要录制视频并将此录制的视频文件发送到服务器,您可以使用AVFoundation框架,如下所示: AVFoundation视频录制

AVCaptureMovieFileOutput *movieFileOutput; 

AVCaptureMovieFileOutput implements the complete file recording interface declared by AVCaptureFileOutput for writing media data to QuickTime movie files. AVCaptureMovieFileOutput实现了AVCaptureMovieFileOutput声明的完整文件记录接口,用于将媒体数据写入QuickTime电影文件。

for starting a recoding you have to call below code with passing output file path as parameter with delegate. 要开始重新编码,您必须调用下面的代码,并将输出文件路径作为参数传递给委托。

//Define output file path.
NSString * filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:["recordVideo" stringByAppendingPathExtension:@"mov"]];

 //Start recording 
[movieFileOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:filePath] recordingDelegate:self];

And after a some time if you want to stop recording then call 一段时间后,如果您想停止录制,请致电

//Stop a recording
[movieFileOutput stopRecording];

after a stop recording delegate methode of AVCaptureFileOutputRecordingDelegate is call so, there you can get recorded file path. 调用AVCaptureFileOutputRecordingDelegate的停止记录委托方法后,可以获取记录的文件路径。

#pragma mark - AVCaptureFileOutputRecordingDelegate Methods.
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error
{

    NSLog(@"Did finish recording, error %@ | path %@ | connections %@", error, [outputFileURL absoluteString], connections);

    //call upload video APi
    [self VideoUploadApi];

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM