区分录制的视频和选定的视频

Tell the difference between a recorded and selected 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);
}

这会将它保存到我的 phone 图书馆。

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

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

在头文件中:

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);
        }
    }
}

现在是最简洁的答案,但它满足了它的需要