0

i use this method to generate the frames of the video

-(NSMutableArray *)generateThumbnailsFromUrl:(NSURL *)videoURl{

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

Float64 durationFrames = CMTimeGetSeconds([asset duration]) * 30.0;

AVMutableComposition *myComp = [AVMutableComposition composition];

AVMutableCompositionTrack *compositionVideoTrack = [myComp addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

NSError *error = nil;
BOOL ok = NO;
CGImageRef imgRef;
UIImage *currentImg;
CMTime time,startTime,endTime;
CMTimeRange myRange;
AVAssetTrack *sourceVideoTrack;

NSMutableArray* frameArray = [[NSMutableArray alloc] init];

AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:myComp];
@try {

for (int i = 0; i < floor(durationFrames); i++) {

    startTime = CMTimeMake(i, 30);
    endTime = CMTimeMake(i+1, 30);

    myRange = CMTimeRangeMake(startTime, endTime);

    sourceVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

    ok = [compositionVideoTrack insertTimeRange:myRange ofTrack:sourceVideoTrack atTime:kCMTimeZero error:&error];

    if (!ok) {
        // Deal with the error.
        NSLog(@"error %@ on %d",error,i);
    }else{
    time = CMTimeMake(0,1);

    imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&error];
    if(imgRef != nil)
    {
     currentImg = [[UIImage alloc] initWithCGImage:imgRef];

    if(currentImg!=nil)
        [frameArray addObject:currentImg];
    }
}
        currentImg=nil;
        CGImageRelease(imgRef);
        ok=NO;
    }
}
    @catch (NSException *exception) {
        NSLog(@"exption is %@",exception);
    }
return frameArray;}

but i have a serious problem in this line exactly imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&error];

sometimes in the middle of execution freeze here , without exception , result and without continue execution ! anyone can help me or explain to me the problem ?

and is there is a way to check if the execution of the command is more a specific time and kill that command ?

Scorpioo.4
  • 45
  • 1
  • 10

1 Answers1

0

According to this solution

you could

[imageGenerator generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error) {

      if (result == AVAssetImageGeneratorSucceeded)
      {
         UIImage *generatedImage = [[UIImage alloc] initWithCGImage:image];

         [imagesArray addObject:generatedImage];
     }
   }];
Community
  • 1
  • 1
loretoparisi
  • 15,724
  • 11
  • 102
  • 146