在保存到照片卷之前为视频文件指定一个特定名称 - iOS

时间:2013-06-07 15:40:04

标签: ios video photo

我正在开发iOS应用程序,最近我在popover中实现了一个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];

    }
}

如果可能的话,我还想避免创建自定义库或自定义元数据。我真的只想在去照片卷的路上更改文件名

1 个答案:

答案 0 :(得分:1)

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