IOS - UISaveVideoAtPathToSavedPhotosAlbum 如何返回保存的視頻路徑? (IOS -how could UISaveVideoAtPathToSavedPhotosAlbum return the saved video path?)


問題描述

IOS ‑ UISaveVideoAtPathToSavedPhotosAlbum 如何返回保存的視頻路徑? (IOS ‑how could UISaveVideoAtPathToSavedPhotosAlbum return the saved video path?)

‑ (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];


    //get the videoURL 
    NSString *tempFilePath = [videoURL path];


    if ( UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(tempFilePath))
    {
      // Copy it to the camera roll.
      UISaveVideoAtPathToSavedPhotosAlbum(tempFilePath, self, @selector(video:didFinishSavingWithError:contextInfo:), tempFilePath);
    } 
}

I use  UISaveVideoAtPathToSavedPhotosAlbum to save video recorded. And I want to know the absolute path in the album where I saved the recorded video.

How could I get the saved Path? UISaveVideoAtPathToSavedPhotosAlbum does not return any.

and in the callback function  video:didFinishSavingWithError:contextInfo: there is still not path info.

‑‑‑‑‑

參考解法

方法 1:

As far as I know..  You cannot get the path for the videos saved in the Photo album.  If you want the files list to  be replayed from your app.  You can have all the videos inside your application.

Following is your sample code to put inside didFinishPickingMedia to store the videos in side documents..  So that you can keep track of it.

   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d", 1]];
    NSString *fileName = [NSString stringWithFormat:@"%@ :%@.%@", itsIncidentType, [dateFormatter stringFromDate:incidentDate], @"mp4"];
    [dateFormatter release];
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
    NSURL *videoURL = [imageInfo objectForKey:UIImagePickerControllerMediaURL];
    NSData *webData = [NSData dataWithContentsOfURL:videoURL];
    self.itsVideoName = fileName;
    [webData writeToFile:[NSString stringWithFormat:@"%@/%@",dataPath,fileName] atomically:TRUE];

Hope this helps you..

方法 2:

I recently had a similar problem and solved it that way.

Create category of PHAsset with two methods

PHAsset+Picking.h

#import <Photos/Photos.h>

@interface PHAsset (Picking)

+ (PHAsset *)retrievePHAssetWithLocalIdentifier:(NSString *)identifier;

+ (void)saveVideoFromCameraToPhotoAlbumWithInfo:(NSDictionary *)info
                                     completion:(void(^)(PHAsset * _Nullable asset))completion;

@end

PHAsset+Picking.m

@implementation PHAsset (Picking)

+ (PHAsset *)retrievePHAssetWithLocalIdentifier:(NSString *)identifier {
    PHAsset *asset = nil;
    if (identifier) {
        PHFetchResult *result = [PHAsset fetchAssetsWithLocalIdentifiers:@[identifier] options:nil];
        asset = result.firstObject;
    }

    return asset;
}

+ (void)saveVideoFromCameraToPhotoAlbumWithInfo:(NSDictionary *)info 
                                     completion:(void(^)(PHAsset * _Nullable asset))completion
{
    // get URL to file from picking media info
    NSURL *url = info[UIImagePickerControllerMediaURL];
    __block PHAssetChangeRequest *changeRequest = nil;
    __block PHObjectPlaceholder *assetPlaceholder = nil;
    // save video file to library
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        changeRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:url];
        assetPlaceholder = changeRequest.placeholderForCreatedAsset;
    } completionHandler:^(BOOL success, NSError *error) {
        if (success) {
            // get saved object as PHAsset 
            PHAsset *asset = [PHAsset retrievePHAssetWithLocalIdentifier:assetPlaceholder.localIdentifier];
            completion(asset);
        } else {
            completion(nil);
        }
    }];
}

@end

Then use method saveVideoFromCameraToPhotoAlbumWithInfo inside 

‑ (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
        Weakify(self);
        void (^processAsset)(PHAsset *, NSDictionary *) = ^void(PHAsset *asset, NSDictionary *mediaInfo) {
            [PHImageManager.defaultManager requestAVAssetForVideo:asset
                                                      options:nil
                                                resultHandler:^(AVAsset *asset,
                                                                AVAudioMix *audioMix,
                                                                NSDictionary *assetInfo)
             {
                 Strongify(self);
                 if ([asset respondsToSelector:@selector(URL)]) {

                     // URL to video file here ‑‑>
                     NSURL *videoURL = [asset performSelector:@selector(URL)];
                     // <‑‑

                 } else {
                     // asset hasn't property URL
                     // it is can be AVComposition (e.g. user chose slo‑mo video)
                 }
             }];
        };

        if (UIImagePickerControllerSourceTypeCamera == picker.sourceType) {
            // save video from camera
            [PHAsset saveVideoFromCameraToPhotoAlbumWithInfo:info completion:^(PHAsset *asset) {
                // processing saved asset
                processAsset(asset, info);
            }];
        } else {
            // processing existing asset from library
            processAsset(info[UIImagePickerControllerPHAsset], info);
        }
    } else {
        // image processing
    }
}

(by lingzhaoDilip Rajkumaruser3747329)

參考文件

  1. IOS ‑how could UISaveVideoAtPathToSavedPhotosAlbum return the saved video path? (CC BY‑SA 3.0/4.0)

#video #iphone #iOS






相關問題

當前在網站上放置音頻和視頻的最佳方式是什麼? (what's the current best way to put audio and video on a web site?)

相當於PNG的視頻? (Video equivalent of PNG?)

IOS - UISaveVideoAtPathToSavedPhotosAlbum 如何返回保存的視頻路徑? (IOS -how could UISaveVideoAtPathToSavedPhotosAlbum return the saved video path?)

如何使用 c# 編寫一個簡單的視頻播放器,以準確的 fps 播放視頻? (How to use c# to write a simple video player which plays the video with accurate fps?)

Monotouch - MoviePlayer 永遠不會超越“正在加載...” (Monotouch - MoviePlayer never gets beyond "loading...")

Android MediaMetadataRetriever setDataSource 失敗 (Android MediaMetadataRetriever setDataSource failed)

枚舉Windows上所有可用視頻編解碼器的最佳方法? (Best way to enumerate all available video codecs on Windows?)

如何通過上傳單個視頻文件為任何 html5 視頻獲得多種質量(360p、480p、720p)選擇 (how can i get multiple quality(360p,480p, 720p) selection for any html5 video with uploading single video file)

由於不支持的編解碼器,FFmpeg/Avconv 無法複製字幕? (FFmpeg/Avconv unable to copy subtitles due to unsupported codec?)

嵌入 youtube 視頻響應解決方案 (embed youtube video responsive solution)

如何輕鬆識別流是視頻還是圖片【ffmpeg庫】 (How to easily recognize whether stream is video or image [ffmpeg library])

livestream.com 如何顯示存檔的視頻? (how is livestream.com displaying the archived video(s)?)







留言討論