我正在尝试使用 AVAssetWriter 创建一个包含大约 20 个图像的 MPEG4 ..
对于 10 张图片,我可以在我的 app 目录中创建一个文件,但是对于 20 张图片,我无法创建一个文件。
这是我尝试保存图像 mpeg4 格式文件的路径(设备路径):
var/mobile/Applications/FED8E776-92CC-47BC-AA47-845DAED5CBE7/Library/Caches/videoOutput1234videoOutput1234.mp4
这里我的问题是:在应用程序目录中创建的文件夹是否有任何限制,如果有,限制是什么以及如何找到创建文件的大小
这是我的代码:
-(void)writeImageAsMovie:(NSMutableArray *)array toPath:(NSString*)path size:(CGSize)size duration:(int)duration
{
CFDataRef imgData;
CGDataProviderRef imgDataProvider;
CGImageRef image1;
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
NSError *error = nil;
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:path] fileType:AVFileTypeMPEG4 error:&error];
NSParameterAssert(videoWriter);
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:size.width], AVVideoWidthKey,
[NSNumber numberWithInt:size.height], AVVideoHeightKey,
nil];
AVAssetWriterInput* writerInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings] retain];
AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput sourcePixelBufferAttributes:nil];
NSParameterAssert(writerInput);
NSParameterAssert([videoWriter canAddInput:writerInput]);
writerInput.expectsMediaDataInRealTime = YES;
[videoWriter addInput:writerInput];
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];
CVPixelBufferRef buffer = NULL;
int frameCount = 0;
for(int i = 0; i<[array count]; i++)
{
imgData = (CFDataRef)[array objectAtIndex:i];
imgDataProvider = CGDataProviderCreateWithCFData (imgData);
image1 = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);
buffer = [self pixelBufferFromCGImage:image1];
BOOL append_ok = NO;
int j = 0;
while (!append_ok && j < 30)
{
if (adaptor.assetWriterInput.readyForMoreMediaData)
{
printf("appending %d attemp %d\n", frameCount, j);
CMTime frameTime;
int64_t value = [[dataBaseTimeArray objectAtIndex:i] intValue];
int32_t preferredTimeScale = 1;
frameTime = CMTimeMake(value, preferredTimeScale);
append_ok = [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];
}
}
else
{
printf("adaptor not ready %d, %d\n", frameCount, j);
[NSThread sleepForTimeInterval:0.1];
}
j++;
}
if (!append_ok) {
printf("error appending image %d times %d\n", frameCount, j);
}
frameCount++;
CVBufferRelease(buffer);
}
if (AVAssetWriterStatusCompleted) {
[writerInput markAsFinished];
[videoWriter finishWriting];
}
}