任何人都可以告訴我如何從視頻中提取圖像嗎?我已經試過到現在也是我已經請按照下列:如何從視頻中提取圖像?
- Getting iPhone video thumbnails
- Getting a thumbnail from a video url or data in iPhone SDK
- How do I extract a screenshot from a video in the iPhone SDK?
- iPhone Read UIimage (frames) from video with AVFoundation,iphone sdk > 3.0 . Video Thumbnail?
等之後,我已經做到了這一點:
源代碼:
- (void)viewDidLoad
{
videoPicker = [[UIImagePickerController alloc] init];
[videoPicker setDelegate:self];
videoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
NSArray *mediaTypesAllowed = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
videoPicker.mediaTypes = mediaTypesAllowed;
videoPicker.view.hidden = YES;
}
-(IBAction)imgPickerController
{
[self presentModalViewController:videoPicker animated:YES];
videoPicker.view.hidden = NO;
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// e.g.
NSString *tempFilePath = [(NSURL *)[info valueForKey:UIImagePickerControllerMediaURL] absoluteString];
NSLog(@"didFinishPickingMediaWithInfo: %@",tempFilePath);
// e.g. /private/var/mobile/Applications/D1E784A4-EC1A-402B-81BF-F36D3A08A332/tmp/capture/capturedvideo.MOV
tempFilePath = [tempFilePath substringFromIndex:16];
NSLog(@"didFinishPickingMediaWithInfo: %@",tempFilePath);
NSLog(@"===Try to save video to camera roll.===");
NSLog(@"UIVideoAtPathIsCompatibleWithSavedPhotosAlbum: %@",UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(tempFilePath)? @"YES":@"NO");
// Check if the video file can be saved to camera roll.
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(tempFilePath))
{
// YES. Copy it to the camera roll.
UISaveVideoAtPathToSavedPhotosAlbum(tempFilePath, self, @selector(video:didFinishSavingWithError:contextInfo:),(__bridge_retained void *)tempFilePath);
}
[self dismissModalViewControllerAnimated:YES];
}
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(NSString *)contextInfo
{
NSLog(@"didFinishSavingWithError--videoPath in camera roll:%@",videoPath);
NSLog(@"didFinishSavingWithError--videoPath in temp directory:%@",contextInfo);
// The thumbnail jpg should located in this directory.
NSString *thumbnailDirectory = [[contextInfo stringByDeletingLastPathComponent] stringByDeletingLastPathComponent];
// Debug info. list all files in the directory of the video file.
// e.g. /private/var/mobile/Applications/D1E784A4-EC1A-402B-81BF-F36D3A08A332/tmp/capture
NSLog(@"%@",[contextInfo stringByDeletingLastPathComponent]);
NSLog(@"%@",[[[NSFileManager defaultManager] contentsOfDirectoryAtPath:[contextInfo stringByDeletingLastPathComponent] error:nil] description]);
// Debug info. list all files in the parent directory of the video file, i.e. the "~/tmp" directory.
// e.g. /private/var/mobile/Applications/D1E784A4-EC1A-402B-81BF-F36D3A08A332/tmp
NSLog(@"%@",thumbnailDirectory);
NSLog(@"%@",[[[NSFileManager defaultManager] contentsOfDirectoryAtPath:thumbnailDirectory error:nil] description]);
///////////////////
// Find the thumbnail for the video just recorded.
NSString *file,*latestFile;
NSDate *latestDate = [NSDate distantPast];
NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:[[contextInfo stringByDeletingLastPathComponent]stringByDeletingLastPathComponent]];
// Enumerate all files in the ~/tmp directory
while (file = [dirEnum nextObject])
{
// Only check files with jpg extension.
if ([[file pathExtension] isEqualToString: @"jpg"])
{
NSLog(@"***latestDate:%@",latestDate);
NSLog(@"***file name:%@",file);
NSLog(@"***NSFileSize:%@", [[dirEnum fileAttributes] valueForKey:@"NSFileSize"]);
NSLog(@"***NSFileModificationDate:%@", [[dirEnum fileAttributes] valueForKey:@"NSFileModificationDate"]);
// Check if current jpg file is the latest one.
if ([(NSDate *)[[dirEnum fileAttributes] valueForKey:@"NSFileModificationDate"] compare:latestDate] == NSOrderedDescending)
{
latestDate = [[dirEnum fileAttributes] valueForKey:@"NSFileModificationDate"];
latestFile = file;
NSLog(@"***latestFile changed:%@",latestFile);
}
}
}
// The thumbnail path.
latestFile = [NSTemporaryDirectory() stringByAppendingPathComponent:latestFile];
NSLog(@"****** The thumbnail file should be this one:%@",latestFile);
UIImage *img = [[UIImage alloc] initWithContentsOfFile:latestFile];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(150, 150, 100, 100)];
[imgView setImage:img];
imgView.layer.borderWidth = 2.0;
[self.view addSubview:imgView];
// Your code ...
// Your code ...
// Your code ...
}
做的這一切我仍力達到我想要的reach.i力得到圖像後still.I'm卡now.Pls任何人幫我!謝謝:)
你可以告訴我如何得到這個縮略圖的路徑和擴展? –
根據我的示例,您將獲得UIImage對象。然後你可以把它保存爲png或jpg。 –
好的,謝謝你的朋友:) –