我想从 URL 下载视频,并在保存之前重命名该视频的文件。我使用UISaveVideoAtPathToSavedPhotosAlbum
。我必须做什么?
NSData *imageData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@"www.xyz.com/image.mp4"]];
最佳答案
你会做这样的事情:
- (void) downloadVideo
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.xyz.com/image.mp4"]];
NSString *tempPath = [NSString stringWithFormat:@"%@/%@", NSTemporaryDirectory(), temp.mp4];
[imageData writeToFile:tempPath atomically:NO];
UISaveVideoAtPathToSavedPhotosAlbum (@ tempPath, self, @selector(video:didFinishSavingWithError: contextInfo:), nil);
);
- (void) video: (NSString *) videoPath
didFinishSavingWithError: (NSError *) error
contextInfo: (void *) contextInfo {
NSLog(@"Finished saving video with error: %@", error);
}
有两点需要注意:
- 确保指定 URL 的架构。这不是浏览器,它不会尝试自动填充 http 并为您猜测。
- 如果这是在主线程上,则会在
dataWithContentsOfURL:
发生时导致同步停顿。如果主线程停止超过 20 秒,手机将认为应用程序陷入无限循环并终止它。因此,如果此代码位于主线程上,您需要将其移至后台线程或使用 NSURLConnection 移至异步下载。
关于ios - 使用 UISaveVideoAtPathToSavedPhotosAlbum 重命名文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1706276/