【问题标题】:How to find the photo gallery path URL in iOS如何在 iOS 中找到照片库路径 URL
【发布时间】:2014-01-15 03:17:34
【问题描述】:

我正在制作一个 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];

【问题讨论】:

标签: ios video photo-gallery assets


【解决方案1】:

使用UISaveVideoAtPathToSavedPhotosAlbumOfficial Documentation

【讨论】:

  • 我正在使用!正在录制视频。我的问题是,我不知道如何在 iOS 设备中找到该视频的 URL。 if(compatible){ NSLog(@"兼容"); UISaveVideoAtPathToSavedPhotosAlbum([videoURL path], self, @selector(video:didFinishSavingWithError:contextInfo:), NULL); NSLog(@"保存的 %@",videoURL); }
  • 我认为您无法获得该视频的有效 URL。如果你想访问库中的视频,你可以使用 UIImagePickerController。
  • 我在 Apple 开发网站上看到我们可以使用 AVAsset。我将编辑我的问题以显示这部分代码。
【解决方案2】:

要录制视频并将录制的视频文件发送到服务器,您可以使用 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];

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多