MPMoviePlayerControlle thumbnailImageAtTime: timeOption: giving empty UIImage

后端 未结 1 1947
忘掉有多难
忘掉有多难 2021-01-06 19:13

Im using this to get a Preview thumbnail of a Video:

- (void)createThumb {
NSInteger paddingLeft = 22;
NSInteger paddingTop = 22;

CGFloat frameWidth = self.         


        
相关标签:
1条回答
  • 2021-01-06 19:26

    OK i found a solution:

    Instead of using MPMoviePlayerController I use this, it does the job much better:

     NSInteger paddingLeft = 24;
    NSInteger paddingTop = 24;
    
    CGFloat frameWidth = self.preview.frame.size.width - (2 * paddingLeft); 
    CGFloat frameHeight = self.preview.frame.size.height - (2 * paddingTop);
    
    CGSize size = CGSizeMake(frameWidth, frameHeight);
    
    AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:self.fileUrl options:nil];
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] 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 *thumbnail=[[UIImage imageWithCGImage:im] retain];
            thumb = [[thumbnail gtm_imageByResizingToSize:size preserveAspectRatio:YES trimToFit:NO] retain];
    
            NSData *thumbData = UIImageJPEGRepresentation(thumb, 0.2);
    
            [thumbData writeToFile:  [[[NSFileManager defaultManager] contentDirectory] stringByAppendingPathComponent:self.thumbFilename] atomically: NO];
            [generator release];
            [self.preview setImage: thumb];   
        }
        else {
            UIImage *thumbnail=[[UIImage imageNamed:@"video_dummy"] retain];
            thumb = [[thumbnail gtm_imageByResizingToSize:size preserveAspectRatio:YES trimToFit:NO] retain];
    
            NSData *thumbData = UIImageJPEGRepresentation(thumb, 0.2);
    
            [thumbData writeToFile:  [[[NSFileManager defaultManager] contentDirectory] stringByAppendingPathComponent:self.thumbFilename] atomically: NO];
            [generator release];
            [self.preview setImage: thumb];
        }
    };
    
    CGSize maxSize = CGSizeMake(320, 180);
    generator.maximumSize = maxSize;
    [generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];    
    

    So if you want to do this use the AVFramework methods

    0 讨论(0)
提交回复
热议问题