
[英]How can I reduce the file size of a video created with UIImagePickerController?
[英]How can I keep track of media created/chosen by UIImagePickerController?
我正在构建一个iOS应用程序,允许用户从UIImagePickerController上传视频,通过录制或从相机胶卷中选择它们,以及播放所选视频。 我的问题是,如何保留对以这种方式选择的视频的引用? 我想这样做,以便如果视频仍然存在于设备上,我可以使用本地文件而不是流式传输上传的文件。
什么时候
imagePickerController:didFinishPickingMediaWithInfo:
返回,URL在:
[info objectForKey:UIImagePickerControllerMediaURL];
格式为:“file:// localhost / private / var / mobile / Applications / /tmp//trim.z2vLjx.MOV”
我认为“/ tmp /”目录是临时的,因此不适合保存该位置的URL。
我可以通过ALAssetsLibrary获取设备上的所有视频,但由于我没有办法区分它们,这对我没有帮助。 我一直在尝试使用:
[result valueForProperty:ALAssetPropertyDate];
为了区分视频,但我需要一种从UIImagePickerController获取创建日期的方法,以使其有用。
我终于设法找到了解决方案:
-(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);
}];
}
}
按照惯例,通过更彻底地阅读文档找到了解决方案。 希望这会在某些时候帮助别人。
您可以轻松记录设备上的视频。 通过保留数据库(我认为太多)或只是一个包含视频列表的文件。 在该列表中,您可以拥有资产的URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.