繁体   English   中英

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

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

我正在制作一个iOS应用程序,用户可以在其中录制视频,并且一旦完成录制,他就可以将该视频发送到服务器。 到现在为止还挺好! 但是要找到录制的视频真是令人头疼! 如何找到录制视频的正确URL? 在测试期间,我已将视频保存在Supporting Files文件夹中,代码如下

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

 NSData *data = [NSData dataWithContentsOfFile:url];

那么,我该怎么做才能替换照相馆中的那个URL?

在Apple Developer网站上显示,我们可以使用AVAsset来访问照片库。 所以我将其复制到我的代码中,并试图找出答案!

//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 官方文档

要录制视频并将此录制的视频文件发送到服务器,您可以使用AVFoundation框架,如下所示: AVFoundation视频录制

AVCaptureMovieFileOutput *movieFileOutput; 

AVCaptureMovieFileOutput实现了AVCaptureMovieFileOutput声明的完整文件记录接口,用于将媒体数据写入QuickTime电影文件。

要开始重新编码,您必须调用下面的代码,并将输出文件路径作为参数传递给委托。

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

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

一段时间后,如果您想停止录制,请致电

//Stop a recording
[movieFileOutput stopRecording];

调用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