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

时间:2014-01-15 03:17:34

标签: ios video photo-gallery assets

我正在创建一个用户可以录制视频的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];

2 个答案:

答案 0 :(得分:0)

使用UISaveVideoAtPathToSavedPhotosAlbum Official Documentation

答案 1 :(得分:0)

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

AVCaptureMovieFileOutput *movieFileOutput; 

AVCaptureMovieFileOutput实现了AVCaptureFileOutput声明的完整文件记录接口,用于将媒体数据写入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];

}