簡體   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