我正在开发一个应用程序,其中录制的视频上传到youtube。但我需要为用户显示拇指图像,并且在上传操作之前还有一个修剪功能。
对于某些录制文件,我在使用 generateCGImagesAsynchronouslyForTimes
创建缩略图时遇到错误 -(void)generateThumbnailsForAsset:(AVAsset *)asset thumbnailCount:(int)thumbnailCount andCompletionHandler:(void (^)(NSArray* thumbnailsArray))completionHandler
{
NSMutableArray *imagesArray = [[NSMutableArray alloc] init];
_imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
KMSDebugLog(@"generateThumbnailsForAsset");
if (thumbnailCount == 1)
{
_imageGenerator.maximumSize = PREVIEW_IMAGE_SIZE;
}
else
{
_imageGenerator.maximumSize = THUMBNAIL_SIZE;
}
CMTime duration = asset.duration;
AVAssetTrack *videoAssetTrack= [[asset tracksWithMediaType:AVMediaTypeVideo] lastObject];
CGAffineTransform videoTransform = videoAssetTrack.preferredTransform;
UIImageOrientation videoOrientation= UIImageOrientationUp;
if(videoTransform.a == -1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == -1.0)
{
videoOrientation= UIImageOrientationDown;
}
CMTimeValue intervalSeconds = duration.value / thumbnailCount;
KMSDebugLog(@"duration.value :%lld duration.timescale:%d",duration.value,duration.timescale);
CMTime time = kCMTimeZero;
NSMutableArray *times = [NSMutableArray array];
for (NSUInteger i = 0; i < thumbnailCount; i++) {
[times addObject:[NSValue valueWithCMTime:time]];
time = CMTimeAdd(time, CMTimeMake(intervalSeconds, duration.timescale));
}
/*[self.imageGenerator generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime,
CGImageRef cgImage,
CMTime actualTime,
AVAssetImageGeneratorResult result,
NSError *error)*/
[self.imageGenerator generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime,
CGImageRef cgImage,
CMTime actualTime,
AVAssetImageGeneratorResult result,
NSError *error)
{
if (error)
{
KMSDebugLog(@"generateCGImagesAsynchronouslyForTimes Error: %@",error);
completionHandler(imagesArray);
}
else
{
if (cgImage)
{
UIImage *image = [UIImage imageWithCGImage:cgImage];
//Orientation support
//UIImage *image = [UIImage imageWithCGImage:cgImage scale:1.0 orientation:videoOrientation];
// NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *rotatedImage = image;
if(videoTransform.a == -1.0 && videoTransform.b == 0 && videoTransform.c == 0 && videoTransform.d == -1.0)
{
rotatedImage = [self imageRotatedByDegrees:image deg:180];
}
[imagesArray addObject:rotatedImage];
}
if (imagesArray.count == thumbnailCount)
{
dispatch_async(dispatch_get_main_queue(), ^{
completionHandler(imagesArray);
});
}
}
}];
}
错误
Line: 265, generateCGImagesAsynchronouslyForTimes Error: Error Domain=AVFoundationErrorDomain
Code=-11829 "Cannot Open" UserInfo={NSLocalizedDescription=Cannot Open,
NSUnderlyingError=0x176f08b0 {Error Domain=NSOSStatusErrorDomain Code=-12848 "(null)"},
NSLocalizedFailureReason=This media may be damaged.}
我无法弄清楚问题,任何帮助将不胜感激。感谢
答案 0 :(得分:0)
我得到了答案。记录结束后,我刚加了一些延迟。
[self performSelector:@selector(UpdateVideoAndConfigureScreenForURL:) withObject:videoURL afterDelay:0.2];