我正在使用UIImagePicker让用户创建视频并将其裁剪。我需要将该视频拆分成多个帧,并让用户选择其中一个。
为了显示这些帧,我很可能需要将它们转换为UIImage。我该怎么做呢?我必须使用AVFoundation,但我找不到有关如何获取和转换帧的教程。
我是否应该也使用AVFoundation进行图像捕捉?如果是这样,那么我是否必须自己实现裁剪?
我正在使用UIImagePicker让用户创建视频并将其裁剪。我需要将该视频拆分成多个帧,并让用户选择其中一个。
为了显示这些帧,我很可能需要将它们转换为UIImage。我该怎么做呢?我必须使用AVFoundation,但我找不到有关如何获取和转换帧的教程。
我是否应该也使用AVFoundation进行图像捕捉?如果是这样,那么我是否必须自己实现裁剪?
以下是从视频中获取FPS图像的代码:
1)导入
#import <Photos/Photos.h>
2)在viewDidLoad中
videoUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"VfE_html5" ofType:@"mp4"]];
[self createImage:5]; // 5 is frame per second (FPS) you can change FPS as per your requirement.
3)函数
-(void)createImage:(int)withFPS {
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoUrl options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.requestedTimeToleranceAfter = kCMTimeZero;
generator.requestedTimeToleranceBefore = kCMTimeZero;
for (Float64 i = 0; i < CMTimeGetSeconds(asset.duration) * withFPS ; i++){
@autoreleasepool {
CMTime time = CMTimeMake(i, withFPS);
NSError *err;
CMTime actualTime;
CGImageRef image = [generator copyCGImageAtTime:time actualTime:&actualTime error:&err];
UIImage *generatedImage = [[UIImage alloc] initWithCGImage:image];
[self savePhotoToAlbum: generatedImage]; // Saves the image on document directory and not memory
CGImageRelease(image);
}
}
}
-(void)savePhotoToAlbum:(UIImage*)imageToSave {
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:imageToSave];
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
NSLog(@"sucess.");
}
else {
NSLog(@"fail.");
}
}];
}