iOS - 在应用内部保存视频

时间:2013-01-18 16:46:15

标签: ios objective-c cocoa-touch video

我最近在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);
}
}

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

1 个答案:

答案 0 :(得分:1)

好吧,我真的不明白你的目的,但是你可以尝试使用NSFileManager来将文件从一个URL复制到另一个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;
            }
        }];
    }
}