Я заканчиваю приложение для iPhone, которое сохраняет фильм в альбом фотографий.Источник фильма представляет собой массив изображений.У меня есть фильмы, и они довольно хорошо помещают их в альбом фотографий, но в начале у них всегда есть дополнительная зеленая рамка.
Есть идеи?
Я перечиталДокументы в Apple, пошевелили провода и сделали несколько тестов с пронумерованными изображениями, чтобы убедиться, что они не пропускают кадр или что-то в этом роде.все еще не понимаю, как правильно.
//build save path with time
NSMutableString *buildPath = [[NSMutableString alloc] init];
[buildPath setString:@"Documents/"];
[buildPath appendString:@"temporary.mp4"];
NSString *fullPath = [NSHomeDirectory() stringByAppendingPathComponent:buildPath];
[buildPath release];
//if the file already exists, deleate it
NSFileManager *fileMgr = [NSFileManager defaultManager];
if([fileMgr fileExistsAtPath:fullPath]){
if([fileMgr removeItemAtPath:fullPath error:&error] != YES){
//error
}
}
//prepare to write the movie
AVAssetWriter *videoWriter =
[[AVAssetWriter alloc] initWithURL
:[NSURL fileURLWithPath:fullPath]
fileType:AVFileTypeMPEG4
error:&error];
NSParameterAssert(videoWriter);
NSDictionary *videoSettings =
[NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264,AVVideoCodecKey,
[NSNumber numberWithInt:width],AVVideoWidthKey,
[NSNumber numberWithInt:height],AVVideoHeightKey,
nil];
AVAssetWriterInput* writerInput =
[[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings]
retain];
AVAssetWriterInputPixelBufferAdaptor *adaptor =
[AVAssetWriterInputPixelBufferAdaptor
assetWriterInputPixelBufferAdaptorWithAssetWriterInput
:writerInput sourcePixelBufferAttributes
:nil];
NSParameterAssert(writerInput);
NSParameterAssert([videoWriter canAddInput:writerInput]);
[videoWriter addInput:writerInput];
//start writing
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];
CVPixelBufferRef buffer = NULL;
buffer = [self pixelBufferFromCGImage
:[[imageArray objectAtIndex:0] CGImage]
:CGSizeMake(width,height)];
CVPixelBufferPoolCreatePixelBuffer(NULL,adaptor.pixelBufferPool,&buffer);
[adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];
//loop through image array
int y = [imageArray count];
int x = 0;
while(x < y)
{
if(writerInput.readyForMoreMediaData == YES){
CMTime frameTime = CMTimeMake(1,24);
CMTime lastTime = CMTimeMake(x,24);
CMTime presentTime = CMTimeAdd(lastTime,frameTime);
buffer = [self pixelBufferFromCGImage
:[[imageArray objectAtIndex:x] CGImage]
:CGSizeMake(width,height)];
[adaptor appendPixelBuffer:buffer
withPresentationTime:presentTime];
x++;
}
}
//finish writing
[writerInput markAsFinished];
[videoWriter finishWriting];
//clean up
CVPixelBufferPoolRelease(adaptor.pixelBufferPool);
[videoWriter release];
[writerInput release];
//handle after save, save is asynchronous
void(^completionBlock)(NSURL *, NSError *) =
^(NSURL *assetURL, NSError *error)
{
if(error != nil){
//error
}
//remove temp movie file
if([fileMgr removeItemAtPath:fullPath error:&error] != YES){
//error
}
};
//write the movie to photo album
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSURL *filePathURL = [NSURL fileURLWithPath:fullPath isDirectory:NO];
if([library videoAtPathIsCompatibleWithSavedPhotosAlbum:filePathURL]){
[library
writeVideoAtPathToSavedPhotosAlbum:filePathURL
completionBlock:completionBlock];
}
//clean up
[library release];