How can I keep track of media created/chosen by UIImagePickerController?

青春壹個敷衍的年華 提交于 2019-12-24 16:25:42

问题


I'm building an iOS app that allows the user to upload videos from UIImagePickerController, either by recording or choosing them from the Camera Roll, as well as also play the chosen video. My question is, how would I go about keeping a reference to the videos that have been chosen this way? I want to do this so that if the video is still present on the device, I can use the local file rather than streaming the uploaded file.

When

 imagePickerController:didFinishPickingMediaWithInfo:

returns, the URL in:

 [info objectForKey:UIImagePickerControllerMediaURL];

Is in the format of: "file://localhost/private/var/mobile/Applications/ /tmp//trim.z2vLjx.MOV"

I'm lead to believe that the "/tmp/" directory is temporary, and therefore not suitable to save the URL for that location.

I can get all of the videos on the device through ALAssetsLibrary, but because I don't have a way of distinguishing them, this doesn't help me. I've been attempting to use:

[result valueForProperty:ALAssetPropertyDate];

To distinguish the videos, but I need a way of getting the creation date from UIImagePickerController for this to be useful.


回答1:


I've finally managed to find a solution:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];

if(CFStringCompare((CFStringRef) mediaType,  kUTTypeMovie, 0) == kCFCompareEqualTo)
{
    //Dismiss the media picker view
    [picker dismissModalViewControllerAnimated:YES];

    //Get the URL of the chosen content, then get the data from that URL
    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
    NSData *webData = [NSData dataWithContentsOfURL:videoURL];

    //Gets the path for the URL, to allow it to be saved to the camera roll
    NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
    if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath))
    {
        ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];

        //The key UIImagePickerControllerReferenceURL allows you to get an ALAsset, which then allows you to get metadata (such as the date the media was created)
        [lib assetForURL:[info objectForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset) {
            NSLog(@"created: %@", [asset valueForProperty:ALAssetPropertyDate]);
        } failureBlock:^(NSError *error) {
            NSLog(@"error: %@", error);
        }];
    }
}

As per usual, the solution was found by reading the documentation a little more thoroughly. Hopefully this'll help someone else out at some point.




回答2:


You can easily keep a record of the videos you have on the device. Either by keeping a data base (which I think would be too much) or just a file with a list of your videos. In that list, you could have the URL of the assets.



来源:https://stackoverflow.com/questions/8474058/how-can-i-keep-track-of-media-created-chosen-by-uiimagepickercontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!