ALAsset thumbnail at specific timestamp

只愿长相守 提交于 2019-12-12 00:23:34

问题


I'm working working on an iPhone application for uploading video files to a specific platform, and one feature I would really love is to be able to present, say, ten different thumbnails for the same video for the user to pick from.

The problem is, that ALAsset only provides a thumbnail method, which just returns the default thumbnail. I have read through the ALAssetRepresentation and ALAsset documentation and I can't seem to find a way to get a thumbnail for a specific timestamp.

I guess one option would be to use something along the lines of libav to get thumbnails but that seems a little "over the top" for something like this. Can anyone help me on this one?

Best regards,
Nick


回答1:


i think this will help you , and you can also see through this prompt Video File thumbnail timestamp missing in ALAsset

{

  if ([theAsset valueForProperty:ALAssetPropertyType] == ALAssetTypeVideo) {

        // Black semi-transparent background at the bottom of the item
        CGRect containerFrame = CGRectMake(0, frame.size.height - AGIPC_ITEM_HEIGHT, frame.size.width, AGIPC_ITEM_HEIGHT);
        UIView *containerForMovieInfo = [[[UIView alloc] initWithFrame:containerFrame] autorelease];
        containerForMovieInfo.backgroundColor = [UIColor blackColor];
        containerForMovieInfo.alpha = 0.7f;

        // Movie icon on left side
        CGRect movieFrame = CGRectMake(4, 60, 26, 15);
        UIImageView *movieImageView = [[[UIImageView alloc] initWithFrame:movieFrame] autorelease];
        if (IS_IPAD()) {
            movieImageView.image = [UIImage imageNamed:@"AGIPC-Movie-iPad"];
        } else {
            movieImageView.image = [UIImage imageNamed:@"AGIPC-Movie-iPhone"];
        }
        [containerForMovieInfo addSubview:movieImageView];

        // Movie duration on right side
        if ([theAsset valueForProperty:ALAssetPropertyDuration] != ALErrorInvalidProperty) {
            NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
            [formatter setDateFormat:@"mm:ss"];
            CGRect durationFrame = CGRectMake(frame.size.width - 26 - 4, 60, 26, 15);
            UILabel *durationView = [[[UILabel alloc] initWithFrame:durationFrame] autorelease];
            durationView.backgroundColor = [UIColor clearColor];
            durationView.textColor = [UIColor whiteColor];
            durationView.text = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:[[theAsset valueForProperty:ALAssetPropertyDuration] doubleValue]]];
            durationView.font = [UIFont systemFontOfSize:10];
            [containerForMovieInfo addSubview:durationView];
        }

        [self addSubview:containerForMovieInfo];
    }

}

last but not least, you must creat the image of the camera on your own.




回答2:


// Get URL from ALAsset* asset:
NSURL* assetURL = [asset valueForProperty:ALAssetPropertyAssetURL];

// Create AVURLAsset using this URL (assetOptions is optional):
NSDictionary* assetOptions = nil;
// assetOptions = @{AVURLAssetPreferPreciseDurationAndTimingKey : @(YES)};
AVAsset* avAsset = [[AVURLAsset alloc] initWithURL:assetURL options:assetOptions];

// Create generator:
AVAssetImageGenerator* generator = [[AVAssetImageGenerator alloc] initWithAsset:avAsset];
generator.appliesPreferredTrackTransform = YES;

// Create array with CMTimes of thumbnails using your own logic.
// (Use +(NSValue*)valueWithCMTime:(CMTime)time to add CMTime in array).
NSArray* times = [self generateThumbnailTimesForVideo:avAsset];

// Generate thumbnail images asynchronously:
[generator generateCGImagesAsynchronouslyForTimes:times
                                completionHandler:^(CMTime requestedTime,
                                                    CGImageRef image,
                                                    CMTime actualTime,
                                                    AVAssetImageGeneratorResult result,
                                                    NSError* error)
    {
        // This block is performed for each CMTime in times array.
        UIImage* thumbnail = [[UIImage alloc] initWithCGImage:image];
    }
];

Synchronous method to get thumbnail at any time is

// PS: SYNC method:
CGImageRef imgRef = [generator copyCGImageAtTime:time actualTime:NULL error:&error];
UIImage* thumbnail = [[UIImage alloc] initWithCGImage:image];


来源:https://stackoverflow.com/questions/11688938/alasset-thumbnail-at-specific-timestamp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!