2016-07-24 11 views
0

ビデオを選択したり撮影したりした後、UIUImageViewをそのビデオのサムネイルで下の友人の代理人に更新します。 iOS 9でPHAssetObjective 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; 
        }); 
}]; 
関連する問題