【问题标题】:ALAsset thumbnail at specific timestamp特定时间戳的 ALAsset 缩略图
【发布时间】:2012-07-26 04:46:32
【问题描述】:

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

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

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

最好的问候,
尼克

【问题讨论】:

    标签: iphone ios video thumbnails alasset


    【解决方案1】:

    我认为这会对您有所帮助,并且 也可以通过这个提示看到 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];
        }
    

    }

    最后但同样重要的是,您必须自己创建相机的图像。

    【讨论】:

    • 我应该在哪里添加这个方法?很紧急,你可以回复吗?
    • 当你 initWithAsset - (id)initWithAsset:(ALAsset *)theAsset .. {}
    【解决方案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];
        }
    ];
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      相关资源
      最近更新 更多