Я хотел создать видео из набора изображений, где я взял большую часть своего кода со ссылкой на:
Как экспортировать массив UIImage в виде фильма?
Код, который я написал:
NSError *error = nil;
NSLog(@"Gonna start writing");
writer = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:@"ravi4"] fileType:AVFileTypeQuickTimeMovie error:&error];
NSParameterAssert(writer);
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey, [NSNumber numberWithInt:640], AVVideoWidthKey, [NSNumber numberWithInt:480], AVVideoHeightKey, nil];
AVAssetWriterInput *writerInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings] retain];
AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
sourcePixelBufferAttributes:nil];
NSParameterAssert(writerInput);
NSParameterAssert([writer canAddInput:writerInput]);
[writer addInput:writerInput];
[writer startWriting];
[writer startSessionAtSourceTime:kCMTimeZero];
UIImage *image = [UIImage imageNamed:@"back.jpg"];
UIImage *image2 = [UIImage imageNamed:@"arthas.jpg"];
UIImage *resizedImage = [ImageToVideoViewController imageWithImage:image scaledToSize:CGSizeMake(640, 480)];
UIImage *resizedImage2 = [ImageToVideoViewController imageWithImage:image2 scaledToSize:CGSizeMake(640, 480)];
CGImageRef img = [resizedImage CGImage];
CGImageRef img2 = [resizedImage2 CGImage];
CVPixelBufferRef buffer = [ImageToVideoViewController pixelBufferFromCGImage:img size:CGSizeMake(640, 480)];
CVPixelBufferRef buffer2 = [ImageToVideoViewController pixelBufferFromCGImage:img2 size:CGSizeMake(640, 480)];
[adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(0, 1)];
while ((adaptor.assetWriterInput.readyForMoreMediaData)==NO ) {
}
[adaptor appendPixelBuffer:buffer2 withPresentationTime:CMTimeMake(3,1)];
while ((adaptor.assetWriterInput.readyForMoreMediaData)==NO ) {
}
[adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(6, 1)];
while ((adaptor.assetWriterInput.readyForMoreMediaData)==NO )
{
}
[adaptor appendPixelBuffer:buffer2 withPresentationTime:CMTimeMake(9, 1)];
while ((adaptor.assetWriterInput.readyForMoreMediaData)==NO )
{
}
[adaptor appendPixelBuffer:buffer2 withPresentationTime:CMTimeMake(10, 1)];
//[adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(10, 2)];
while ((adaptor.assetWriterInput.readyForMoreMediaData)==NO )
{
}
CVBufferRelease(buffer);
CVBufferRelease(buffer2);
[writerInput markAsFinished];
//[writerInput finishWriting];
[writerInput release];
[writer endSessionAtSourceTime:CMTimeMake(10, 1)];
[writer finishWriting];
[writer release];
Код запутан, так как я просто экспериментирую, как решить эту проблему
Видео идет нормально, кромедля последнего изображения, которое я пытаюсь отобразить, видео останавливается и останавливается, как только требуется отобразить это изображение, но будет работать нормально, пока оно не достигнет этой точки
Спасибо!