使用generateCGImages异步生成缩略图ForTimes冻结屏幕,直到在iOS 8中生成图像

时间:2014-09-25 06:38:11

标签: ios8

我正在尝试使用generateCGImagesAsynchronouslyForTimes从http url生成缩略图。这完全适用于ios 7.1。但是在我升级到iOS 8之后,这不能以异步方式工作。屏幕冻结直到图像生成。

以下是我尝试使用的代码:

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:videoContentIDFullPath] options:nil];

AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;

CMTime thumbTime = CMTimeMakeWithSeconds(30,30);

AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
    if (result != AVAssetImageGeneratorSucceeded) {
        NSLog(@"couldn't generate thumbnail, error:%@", error);
    }
    // TODO Do something with the image
    self.imageview.image = [[UIImage alloc] initWithCGImage:im];;

};

CGSize maxSize = CGSizeMake(128, 128);
generator.maximumSize = maxSize;
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

     __weak typeof(self) weakSelf = self; <--

    AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
        if (result != AVAssetImageGeneratorSucceeded) {
            NSLog(@"couldn't generate thumbnail, error:%@", error);
        }
        dispatch_async(dispatch_get_main_queue(), ^{ <--
            // TODO Do something with the image
            weakSelf.imageview.image = [[UIImage alloc] initWithCGImage:im]; <--
        });

    };