2016-07-24 65 views
0

選擇或拍攝視頻後,我想在下面的同伴代表旁邊用該視頻的縮略圖更新我的UIUImageView。我如何使用PHAsset來做iOS 9和Objective C如何從iOS 9和Objective C中的.MOV文件中獲取縮略圖?

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType]; 
    if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) { 
    NSURL *videoUrl=(NSURL*)[info objectForKey:UIImagePickerControllerMediaURL]; 
    NSData *videoData = [NSData dataWithContentsOfURL:videoUrl]; 
    } 
} 

回答

0
+ (UIImage *)imageFromMovie:(NSURL *)movieURL atTime:(NSTimeInterval)time { 
    // set up the movie player 
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] 
initWithContentURL:movieURL]; 
mp.shouldAutoplay = NO; 
mp.initialPlaybackTime = time; 
mp.currentPlaybackTime = time; 
// get the thumbnail 
UIImage *thumbnail = [mp thumbnailImageAtTime:time 
         timeOption:MPMovieTimeOptionNearestKeyFrame]; 
// clean up the movie player 
[mp stop]; 
[mp release]; 
return(thumbnail); 

}

+0

的MPMoviePlayerController在iOS的8 – Rokon

1
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 

    self.sourceURLString =[NSString stringWithFormat:@"%@", [info               objectForKey:UIImagePickerControllerMediaURL]]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
    self.thumbImageView.image = [self generateThumbImage:self.sourceURLString]; 
    self.addVideoButton.hidden = YES; 
    }); 

} 

-(UIImage *)generateThumbImage : (NSString *)sourceURLString{ 
    NSURL *sourceMovieURL = [NSURL URLWithString:sourceURLString]; 
    AVURLAsset *sourceAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil]; 
    AVAssetImageGenerator *generateImg = [[AVAssetImageGenerator alloc] initWithAsset:sourceAsset]; 
    NSError *error = NULL; 
    CMTime time = CMTimeMake(1, 65); 
    CGImageRef refImg = [generateImg copyCGImageAtTime:time actualTime:NULL error:&error]; 
    NSLog(@"error==%@, Refimage==%@", error, refImg); 

    return [[UIImage alloc] initWithCGImage:refImg]; 
} 
+0

棄用如果您使用PhotoLibrary,那麼-requestImageForAsset可能是更好的方法。 – Andy

3

嘗試用下面的代碼,其中asset參數是你PHAsset實例來做到這一點:

PHImageManager *manager = [PHImageManager defaultManager]; 

[manager requestImageForAsset: asset 
        targetSize: CGSizeMake(100.0, 100.0) // use required size 
        contentMode: PHImageContentModeAspectFill 
         options: nil 
       resultHandler: ^(UIImage * _Nullable anImage, NSDictionary * _Nullable info) { 

        dispatch_async(dispatch_get_main_queue(), ^{ 
         yourImageView.image = anImage; 
        }); 
}]; 
相關問題