[英]Problem saving recorded video on iPhone's library
I am trying to save a recorded video to the library using the UIImagePickerController delegate. 我正在尝试使用UIImagePickerController委托将录制的视频保存到库中。 It works fine for pictures but it doesn't save if it's a video, plus after trying to save a video if I open the Photos app, I get a message "Please wait. Updatring library", and a progress bar with the label "Rebuilding library".
它适用于图片但如果它是视频则无法保存,如果我打开照片应用程序后尝试保存视频,我会收到一条消息“请等待.Update字符串库”,以及带有标签的进度条“重建图书馆“。 My photo library is restored but the video was not added to it.
我的照片库已恢复,但视频未添加到其中。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"]){
UIImage *picture = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(picture, nil, nil, nil);
}
else if ([mediaType isEqualToString:@"public.movie"]){
NSString* m_objMediaURL= [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(@"URL is %@", m_objMediaURL);
UISaveVideoAtPathToSavedPhotosAlbum(m_objMediaURL, nil, nil, nil);
}
[self dismissModalViewControllerAnimated:YES];
}
The NSLog above outputs the following: 上面的NSLog输出如下:
URL is
file://localhost/private/var/mobile/Applications/3800A5FB-2A96-4A7A-85DF-B635E8D9A66C/tmp/capture-T0x1061f0.tmp.JMdxHq/capturedvideo.MOV
Has anyone implemented such method and can point me to the right direction please? 有没有人实施过这样的方法,能指出正确的方向吗?
Thank you. 谢谢。
I think I see an issue. 我想我看到了一个问题。
NSString* m_objMediaURL= [info objectForKey:UIImagePickerControllerMediaURL];
Is the wrong type. 是错误的类型。 My properly working code uses:
我正常工作的代码使用:
NSURL *url = [[[info objectForKey:UIImagePickerControllerMediaURL] copy] autorelease];
I copy it instead of retaining it because of some issues I have had in low memory situations after the picker view is dismissed. 我复制它而不是保留它,因为在拾取器视图被解除后我在低内存情况下遇到了一些问题。 Although the save method you use might work, I use the newer and better (because of the callback block):
虽然您使用的保存方法可能有效,但我使用的是更新更好(因为回调块):
ALAssetsLibrary* library = [[[ALAssetsLibrary alloc] init] autorelease];
[library writeVideoAtPathToSavedPhotosAlbum:url
completionBlock:^(NSURL *assetURL, NSError *error){/*notify of completion*/}];
Also, you should be using kUTTypeMovie
and kUTTypeImage
instead of hard coding @"public.image
. 此外,您应该使用
kUTTypeMovie
和kUTTypeImage
而不是硬编码@"public.image
。
I found the problem thanks to Peter's help. 彼得的帮助让我发现了这个问题。 Turns out that [info objectForKey:UIImagePickerControllerMediaURL] returns a NSURL and not a NSString.
事实证明[info objectForKey:UIImagePickerControllerMediaURL]返回NSURL而不是NSString。 So in the end here's how my method looks like
所以最后这是我的方法的样子
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSURL *videoURL= [info objectForKey:UIImagePickerControllerMediaURL];
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:videoURL])
{
[library writeVideoAtPathToSavedPhotosAlbum:videoURL
completionBlock:^(NSURL *assetURL, NSError *error){}
];
}
[library release];
This method uses the new ALAssetsLibrary framework. 此方法使用新的ALAssetsLibrary框架。 The whole method, which supports saving a picture or movie, follows:
支持保存图片或电影的整个方法如下:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"]){
UIImage *picture= [info objectForKey:UIImagePickerControllerOriginalImage];
NSDictionary *metadata = [info objectForKey:UIImagePickerControllerMediaMetadata];
[library writeImageToSavedPhotosAlbum:[picture CGImage] metadata:metadata
completionBlock:^(NSURL *assetURL, NSError *error){}
];
}
else if ([mediaType isEqualToString:@"public.movie"]){
NSURL *videoURL= [info objectForKey:UIImagePickerControllerMediaURL];
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:videoURL])
{
[library writeVideoAtPathToSavedPhotosAlbum:videoURL
completionBlock:^(NSURL *assetURL, NSError *error){}
];
}
}
[library release];
[self dismissModalViewControllerAnimated:YES];
}
Why are you passing nil, nil, nil into the method - if you pass in more options, it will tell you the error ;) 为什么你将nil,nil,nil传递给方法 - 如果传入更多选项,它会告诉你错误;)
Try something like this instead : 尝试这样的事情:
UISaveVideoAtPathToSavedPhotosAlbum(m_objMediaURL, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
and later on in your class put this method 然后在你班上放这个方法
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
// Let's see what happened
NSLog(@"path : %@", path);
NSLog(@:error : %@", error);
}
Hope the output of that helps you :) 希望它的输出可以帮助你:)
EDIT : 编辑:
Reading the docs a bit more, have you run this method to check that it's a valid video? 更多地阅读文档 ,您是否运行此方法来检查它是否是有效的视频?
BOOL isVideoOK = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(m_objMediaURL);
if (NO == isVideoOK)
NSLog(@"Video at %@ is not compatible", m_objMediaURL);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.