区分录制和选定的视频

时间:2015-01-23 05:34:27

标签: ios objective-c video

我正致力于将视频添加到聊天应用程序中。用户可以共享从其文件中选择的视频,也可以录制新的视频进行上传。

我遇到的问题是区分刚刚录制的视频和已选择的视频,因为我想在上传时将刚刚录制的视频保存到相册中。

目前我的代码如下:

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

    // This checks whether we are adding image or video (public.movie for video)
    if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:@"public.image"]) {

        UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];

        [self sendImage:image];
    }
    else {

        // SS-V
        // If we are dealing with a video then we want to return to the chat view and post the video
        NSURL * videoURL = (NSURL *)[info objectForKey:UIImagePickerControllerMediaURL];

        // Send video to the chat view
        [self sendVideo:[videoURL absoluteString]];
    }

    [tableView reloadData];
    [picker dismissViewControllerAnimated:YES completion:Nil];
}

所以我获取了视频的本地URL,然后将其上传到外部数据库。

我也有这段代码:

// Save the recorded video to the iPhone
NSString * videoCompatibleString = [videoURL.absoluteString stringByReplacingOccurrencesOfString:@"file://" withString:@""];

if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(videoCompatibleString)) {

    UISaveVideoAtPathToSavedPhotosAlbum (videoCompatibleString, self, nil, nil);
}

将保存到我的手机库。

所以现在我唯一的问题是如何区分刚拍摄的视频和从我的图书馆中选择的视频。

1 个答案:

答案 0 :(得分:0)

我希望有一种优雅的方式来实现这一目标。似乎没有,所以我设置了一个枚举记录我加载图像选择器的内容,如果我用相机加载它然后我知道我将拍摄视频或图片所以应该将它添加到相册:< / p>

在头文件中:

typedef enum {
    bPictureTypeCamera,
    bPictureTypeAlbumImage,
    bPictureTypeAlbumVideo,
} bPictureType;

// This allows us to see what kind of media is being sent to know if we need to save it to album
bPictureType _pictureType;

在主文件中:

// If we have just taken the media then save it to the users camera
if (_pictureType == bPictureTypeCamera) {
    [self saveMediaWithInfo:info];
}

使用我的保存功能检查它是视频还是图像并保存:

- (void)saveMediaWithInfo: (NSDictionary *)info {

    // Save image
    if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:@"public.image"]) {

        UIImageWriteToSavedPhotosAlbum([info objectForKey:UIImagePickerControllerEditedImage], self, nil, nil);
    }
    // Save Video
    else {

        // Make sure the video is in a compatible format to save
        NSURL * videoURL = (NSURL *)[info objectForKey:UIImagePickerControllerMediaURL];
        NSString * videoCompatibleString = [[videoURL absoluteString] stringByReplacingOccurrencesOfString:@"file://" withString:@""];

        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (videoCompatibleString)) {

            UISaveVideoAtPathToSavedPhotosAlbum (videoCompatibleString, self, nil, nil);
        }
    }
}

现在是最好的答案,但它确实需要