【问题标题】:Tell the difference between a recorded and selected video分辨录制的视频和选定的视频之间的区别
【发布时间】:2015-01-23 05:34:27
【问题描述】:

我正在向聊天应用程序添加视频。用户可以分享从他们的文件中选择的视频,也可以录制一个新视频以上传。

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

目前我的代码如下所示:

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

这会将它保存到我的手机库中。

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

【问题讨论】:

    标签: ios objective-c video


    【解决方案1】:

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

    在头文件中:

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

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

    【讨论】:

      猜你喜欢
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      • 2019-05-31
      • 1970-01-01
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多