iOS 视频的每一帧图片关键帧

#import

#import

#import

// 方法一

- (void)movieToImage{

    

    NSURL *url = [NSURL URLWithString:movieURL];

    

    AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:url options:nil];

    AVAssetImageGenerator *generator = [[AVAssetImageGeneratoralloc] initWithAsset:asset];

    generator.appliesPreferredTrackTransform=TRUE;

    [asset release];

    CMTime thumbTime =CMTimeMakeWithSeconds(0,30);

    

    AVAssetImageGeneratorCompletionHandler handler =

    ^(CMTime requestedTime,CGImageRef im, CMTime actualTime,AVAssetImageGeneratorResult result,NSError *error){

        if (result !=AVAssetImageGeneratorSucceeded) {       }//没成功

        

        UIImage *thumbImg = [[UIImageimageWithCGImage:im] retain];

        [generator release];

        

        [selfperformSelectorOnMainThread:@selector(movieImage:)withObject:thumbImg waitUntilDone:YES];

        

    };

    

    generator.maximumSize =self.frame.size;

    [generator generateCGImagesAsynchronouslyForTimes:

     [NSArrayarrayWithObject:[NSValuevalueWithCMTime:thumbTime]]completionHandler:handler];

}


//方法二

//MPMoviePlayerController 类已经提供了得到帧图片和方法

- (UIImage *)thumbnailImageAtTime:(NSTimeInterval)playbackTime timeOption:(MPMovieTimeOption)optionNS_DEPRECATED_IOS(3_2,7_0);

//使用:

[movieImage setImage:[tempMovie thumbnailImageAtTime:2.5f timeOption:MPMovieTimeOptionNearestKeyFrame]];


你可能感兴趣的:(iOS)