[英]How can I keep track of media created/chosen by UIImagePickerController?
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. 我正在构建一个iOS应用程序,允许用户从UIImagePickerController上传视频,通过录制或从相机胶卷中选择它们,以及播放所选视频。 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: 返回,URL在:
[info objectForKey:UIImagePickerControllerMediaURL];
Is in the format of: "file://localhost/private/var/mobile/Applications/ /tmp//trim.z2vLjx.MOV" 格式为:“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. 我认为“/ tmp /”目录是临时的,因此不适合保存该位置的URL。
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. 我可以通过ALAssetsLibrary获取设备上的所有视频,但由于我没有办法区分它们,这对我没有帮助。 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. 为了区分视频,但我需要一种从UIImagePickerController获取创建日期的方法,以使其有用。
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.
希望这会在某些时候帮助别人。
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.
在该列表中,您可以拥有资产的URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.