我想在单击视频按钮时从photoGallery获取视频,并在同一视图中显示。我已经编写了这段代码,但是没有将视频放到同一视图的imageview中。
- (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,所以任何人都可以帮我做到这一点。任何帮助都会更受欢迎。
答案 0 :(得分:2)
我们无法在UIImageView中直接显示视频。如果我们尝试直接加载,它将仅显示黑屏。要在UIImageView中显示视频,我们需要加载第一帧视频。为此需要导入两个框架。
1.AVFoundation
2.AssetsLibrary
使用这两个我们可以将第一帧视频显示到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];
}