【问题标题】:iOS - Saving Video Inside AppiOS - 在应用内保存视频
【发布时间】:2013-01-18 16:46:15
【问题描述】:

我最近在 stackoverflow 上找到了一个关于如何在 App 中存储图像的答案:

-(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
if ([[extension lowercaseString] isEqualToString:@"png"]) {
    [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
} else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
    [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
} else {
    ALog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);
}
}

如何存储用户从照片/视频库中选择的视频并将其保存在应用程序中?有什么不同吗?我正在尝试跟踪用户想要上传的视频列表,当他/她准备好时,他们可以将所有这些视频上传到应用程序的服务器。

【问题讨论】:

  • 为什么要单独保存视频副本?为什么不缓存视频路径,然后上传这些原件?
  • 正如 Miles Alden 所说,您可能想要跟踪电影 URL,而不是复制整个电影。在您的选择器委托中,您可以使用 UIImagePickerControllerMediaURL 键从信息字典中获取 URL。
  • 有道理。我会试试看。我可以使用 NSUserDefault?

标签: ios objective-c cocoa-touch video


【解决方案1】:

好吧,我不太了解您的目的,但您可以尝试使用 NSFileManager 将文件从一个 URL 复制到另一个:

NSError *error;
[[NSFileManager defaultManager] copyItemAtURL:mediaURL toURL:outputURL error:&error];

如果您需要以某种方式处理您的电影(即裁剪、更改质量),您应该使用 AVExportSession:

+ (void)writeMovieAtURL:(NSURL *)mediaURL
                  toURL:(NSURL *)outputURL
             withQality:(NSInteger)quality
      completionHandler:(void (^)(NSURL *, NSError *))completion {


    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:mediaURL options:nil];


    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset];
    NSString *exportPreset;

    switch (quality) {
        case 0 : { exportPreset = AVAssetExportPreset1280x720;      } break;
        case 1 : { exportPreset = AVAssetExportPreset640x480;       } break;
        case 2 : { exportPreset = AVAssetExportPresetMediumQuality; } break;
        case 3 : { exportPreset = AVAssetExportPresetLowQuality;    } break;
    }

    if (nil != exportPreset && [compatiblePresets containsObject:exportPreset]) {
        AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
                                  initWithAsset:asset presetName:exportPreset];

        exportSession.outputURL = outputURL;

        exportSession.outputFileType = AVFileTypeQuickTimeMovie;

        // set some params to the session

        [exportSession exportAsynchronouslyWithCompletionHandler:^{                
            switch ([exportSession status]) {
                case AVAssetExportSessionStatusCompleted: {                       
                    completion(outputURL, nil);
                    [exportSession release];
                } break;
                case AVAssetExportSessionStatusWaiting : {
                    NSLog(@"Export Waiting");
                } break;
                case AVAssetExportSessionStatusExporting : {
                    NSLog(@"Export Exporting");
                } break;
                case AVAssetExportSessionStatusFailed : {
                    completion(outputURL, [exportSession error]);
                    [exportSession release];
                } break;
                case AVAssetExportSessionStatusCancelled : {
                    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"Movie export session canceled"
                                                                         forKey:@"ocalizedDescription"];                        
                    NSError *error = [NSError errorWithDomain:@"your.domain" 
                                                         code:123 
                                                     userInfo:userInfo];                        
                    completion(outputURL, error);
                    [exportSession release];
                } break;
            }
        }];
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多