【问题标题】:Giving a video file a specific name before saving to photo roll - iOS在保存到照片卷之前为视频文件指定特定名称 - iOS
【发布时间】:2013-06-07 15:40:03
【问题描述】:

我正在开发一个 iOS 应用程序,我最近在弹出窗口中实现了一个 imagePicker,它允许用户录制照片和视频。我想将这些存储在 iPad 的相册中,以便用户稍后可以在 iTunes 中同步它们。我已经能够成功地做到这一点,但我想给每张照片和视频一个唯一的名称,其中包含有关照片和视频的有用信息,以便我以后可以加载它们。具体来说,我想使用类的属性 live_trial_id 作为文件名来存储照片。以下是我目前用于将照片和视频存储到相册的代码。我知道我可以使用图片的元数据来做到这一点,但对于视频我迷路了。提前感谢您对此问题的任何帮助!

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

NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;

// Handle a still image capture
if( [mediaType isEqualToString:@"public.image"]){

    editedImage = (UIImage *) [info objectForKey:
                               UIImagePickerControllerEditedImage];
    originalImage = (UIImage *) [info objectForKey:
                                 UIImagePickerControllerOriginalImage];

    if (editedImage) {
        imageToSave = editedImage;
    } else {
        imageToSave = originalImage;
    }

    // Get the image metadata
    UIImagePickerControllerSourceType pickerType = picker.sourceType;
    if(pickerType == UIImagePickerControllerSourceTypeCamera)
    {
        NSDictionary *imageMetadata = [info objectForKey:
                                       UIImagePickerControllerMediaMetadata];
        // Get the assets library
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        ALAssetsLibraryWriteImageCompletionBlock imageWriteCompletionBlock =
        ^(NSURL *newURL, NSError *error) {
            if (error) {
                NSLog( @"Error writing image with metadata to Photo Library: %@", error );
            } else {
                NSLog( @"Wrote image with metadata to Photo Library");
            }
        };

        // Save the new image (original or edited) to the Camera Roll
        [library writeImageToSavedPhotosAlbum:[imageToSave CGImage]
                                     metadata:imageMetadata
                              completionBlock:imageWriteCompletionBlock];
    }
}

if ([mediaType isEqualToString:@"public.movie"]) {
    UIImagePickerControllerSourceType pickerType = picker.sourceType;
    if(pickerType == UIImagePickerControllerSourceTypeCamera)
    {
        NSURL *mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
        // Get the assets library
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        ALAssetsLibraryWriteVideoCompletionBlock videoWriteCompletionBlock =
        ^(NSURL *newURL, NSError *error) {
            if (error) {
                NSLog( @"Error writing image with metadata to Photo Library: %@", error );
            } else {
                NSLog( @"Wrote image with metadata to Photo Library");
            }
        };

        // Save the new image (original or edited) to the Camera Roll
        [library writeVideoAtPathToSavedPhotosAlbum:mediaURL
                              completionBlock:videoWriteCompletionBlock];

    }
}

我还想尽可能避免创建自定义库或自定义元数据。我真的只是想在去相册的路上更改文件名

【问题讨论】:

  • 感谢您的代码帮助我识别正在捕获的视频或图像 ;-)

标签: ios video photo


【解决方案1】:

我最终回答了我自己的问题。我想做的是保存到应用程序的文档目录而不是照片卷。这使我可以访问已保存的文件,并在以后附加时将它们同步到计算机。

【讨论】:

    猜你喜欢
    • 2013-07-22
    • 2010-12-29
    • 2014-09-30
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多