, , , 1,0 , , , CMTime.
CMTime requestTime = CMTimeMakeWithSeconds(imageTime, 1);
CMTime requestTime = CMTimeMakeWithSeconds(imageTime, playerItem.asset.duration.timescale);
... Boom, :)
, , , , , :
NSValue * time = [NSValue valueWithCMTime:CMTimeMakeWithSeconds(i*frameDuration, composition.frameDuration.timescale*2)];
:
playerItem = [AVPlayerItem playerItemWithURL:item.movieUrl];
imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:playerItem.asset];
imageGenerator.requestedTimeToleranceAfter = kCMTimeZero;
imageGenerator.requestedTimeToleranceBefore = kCMTimeZero;
float duration = item.duration;
float interval = item.interval;
NSLog(@"\nItem info:\n%f \n%f", duration,interval);
NSString *srcPath = nil;
NSString *zipPath = nil;
srcPath = [item.path stringByAppendingPathComponent:@"info.json"];
zipPath = [NSString stringWithFormat:@"/%@/info.json",galleryID];
[zip addFileToZip:srcPath newname:zipPath level:0];
NSTimeInterval frameNum = item.duration / item.interval;
for (int i=0; i<=frameNum; i++)
{
NSArray* cachePathArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString* cachePath = [cachePathArray lastObject];
srcPath = [cachePath stringByAppendingPathComponent:@"export-tmp.jpg"];
zipPath = [NSString stringWithFormat:@"/%@/%d.jpg",galleryID,i];
float imageTime = ( i * interval );
NSError *error = nil;
CMTime requestTime = CMTimeMakeWithSeconds(imageTime, playerItem.asset.duration.timescale);
CMTime actualTime;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:requestTime actualTime:&actualTime error:&error];
if (error == nil) {
float req = ((float)requestTime.value/requestTime.timescale);
float real = ((float)actualTime.value/actualTime.timescale);
float diff = fabsf(req-real);
NSLog(@"copyCGImageAtTime: %.2f, %.2f, %f",req,real,diff);
}
else
{
NSLog(@"copyCGImageAtTime: error: %@",error.localizedDescription);
}
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
[UIImageJPEGRepresentation(img, 100) writeToFile:srcPath atomically:YES];
if (srcPath != nil && zipPath!= nil)
{
[zip addFileToZip:srcPath newname:zipPath level:0];
unlink([srcPath UTF8String]);
}