0

我正在开发一个用于将视频文件上传到特定平台的 iPhone 应用程序,我真正喜欢的一个功能是能够为同一视频呈现十个不同的缩略图供用户选择。

问题是,ALAsset 只提供了一个缩略图方法,它只返回默认的缩略图。我已通读 ALAssetRepresentation 和 ALAsset 文档,但似乎找不到获取特定时间戳缩略图的方法。

我想一个选择是使用类似于 libav 的东西来获取缩略图,但这似乎有点“过头”了。谁能帮我解决这个问题?

最好的问候,
尼克

4

2 回答 2

4

我认为这会对您有所帮助,您还可以通过此提示 视频文件缩略图时间戳在 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];
    }

}

最后但并非最不重要的一点是,您必须自己创建相机的图像。

于 2012-08-20T14:23:53.847 回答
0
// 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];
    }
];

随时获取缩略图的同步方法是

// PS: SYNC method:
CGImageRef imgRef = [generator copyCGImageAtTime:time actualTime:NULL error:&error];
UIImage* thumbnail = [[UIImage alloc] initWithCGImage:image];
于 2014-02-05T15:35:12.737 回答