如何在iOS中使用UIImagePickerController从photoGallery获取视频?
我想在单击视频按钮时从photoGallery中获取视频,该按钮显示在同一视图中。我已经写了这段代码,但是没有在同一个视图上把视频放到imageview中如何在iOS中使用UIImagePickerController从photoGallery获取视频?,ios,uiimage,Ios,Uiimage,我想在单击视频按钮时从photoGallery中获取视频,该按钮显示在同一视图中。我已经写了这段代码,但是没有在同一个视图上把视频放到imageview中 - (IBAction)vedioClicked:(id)sender { imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.allowsEditing = YES; imagePicker.sourc
- (IBAction)vedioClicked:(id)sender
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes =
[[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
[self presentViewController:imagePicker animated:YES completion:nil];
}
- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
{
UIImage *editedImage = (UIImage *)[info objectForKey:@"UIImagePickerControllerOriginalImage"];
// Compressing the image size
CGSize newSize = CGSizeMake(400, 400);
UIGraphicsBeginImageContext(newSize);
[editedImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Get the new image from the context
inputImage = UIGraphicsGetImageFromCurrentImageContext();
self.imageView.image=inputImage;
// End the context
UIGraphicsEndImageContext();
NSData *data = UIImageJPEGRepresentation(inputImage,0.2);//PNGRepresentation(imgView.image);
}
else if ([mediaType isEqualToString:@"public.movie"]){
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
}
self.imageView.image=inputImage;
[picker dismissViewControllerAnimated:YES completion:nil];
}
视频是从照片库获取的,但它并没有显示在同一视图的imageView中,所以任何人都可以帮助我做到这一点。任何帮助都将不胜感激 我们无法在UIImageView中直接显示视频。如果我们尝试直接加载,它将只显示黑屏。要在UIImageView中显示视频,我们需要加载视频的第一帧。为此,需要导入两个框架 1.AVF基金会 2.3资产库 使用这两个选项,我们可以将视频的第一帧显示到UIImageView中,如下所示:
- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
{
UIImage *editedImage = (UIImage *)[info objectForKey:@"UIImagePickerControllerOriginalImage"];
// Compressing the image size
CGSize newSize = CGSizeMake(400, 400);
UIGraphicsBeginImageContext(newSize);
[editedImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Get the new image from the context
inputImage = UIGraphicsGetImageFromCurrentImageContext();
self.imageView.image=inputImage;
// End the context
UIGraphicsEndImageContext();
NSData *data = UIImageJPEGRepresentation(inputImage,0.2);//PNGRepresentation(imgView.image);
}
else if ([mediaType isEqualToString:@"public.movie"])
{
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
AVAsset *avAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
if ([[avAsset tracksWithMediaType:AVMediaTypeVideo] count] > 0)
{
AVAssetImageGenerator *imageGenerator =[AVAssetImageGenerator assetImageGeneratorWithAsset:avAsset];
Float64 durationSeconds = CMTimeGetSeconds([avAsset duration]);
CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);
NSError *error;
CMTime actualTime;
CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:kCMTimeZero actualTime:&actualTime error:&error];
if (halfWayImage != NULL)
{
NSString *actualTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, actualTime));
NSString *requestedTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, midpoint));
NSLog(@"Got halfWayImage: Asked for %@, got %@", requestedTimeString, actualTimeString);
UIImage *img=[UIImage imageWithCGImage:halfWayImage];
self.imageView.image=img;
}
}
}
[picker dismissViewControllerAnimated:YES completion:nil];
}