所以我设法在网上找到一些代码来完成下面显示的工作:
- (voID)writeImagesAsMovIE:(NSArray *)array {NSArray *paths = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,NSUserDirectory,YES);Nsstring *documentDirectory = [paths objectAtIndex:0];Nsstring *saveLocation = [documentDirectory stringByAppendingString:@"/temp.mov"];if ([[NSfileManager defaultManager] fileExistsAtPath:saveLocation]) { [[NSfileManager defaultManager] removeItemAtPath:saveLocation error:NulL];}UIImage *first = [array objectAtIndex:0];CGSize frameSize = first.size;NSError *error = nil;AVAssetWriter *vIDeoWriter = [[AVAssetWriter alloc] initWithURL: [NSURL fileURLWithPath:saveLocation] fileType:AVfileTypeQuickTimeMovIE error:&error];if(error) { NSLog(@"error creating AssetWriter: %@",[error description]);}NSDictionary *vIDeoSettings = [NSDictionary dictionaryWithObjectsAndKeys: AVVIDeoCodecH264,AVVIDeoCodecKey,[NSNumber numberWithInt:frameSize.wIDth],AVVIDeoWIDthKey,[NSNumber numberWithInt:frameSize.height],AVVIDeoHeightKey,nil];AVAssetWriterinput *writerinput = [AVAssetWriterinput assetWriterinputWithMediaType:AVMediaTypeVIDeo outputSettings:vIDeoSettings];NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];[attributes setobject:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32ARGB] forKey:(Nsstring*)kCVPixelBufferPixelFormatTypeKey];[attributes setobject:[NSNumber numberWithUnsignedInt:frameSize.wIDth] forKey:(Nsstring*)kCVPixelBufferWIDthKey];[attributes setobject:[NSNumber numberWithUnsignedInt:frameSize.height] forKey:(Nsstring*)kCVPixelBufferHeightKey];AVAssetWriterinputPixelBufferAdaptor *adaptor = [AVAssetWriterinputPixelBufferAdaptor assetWriterinputPixelBufferAdaptorWithAssetWriterinput:writerinput sourcePixelBufferAttributes:attributes];[vIDeoWriter addinput:writerinput];// fixes all errorswriterinput.expectsMediaDataInRealTime = YES;//Start a session:[vIDeoWriter startWriting];[vIDeoWriter startSessionAtSourceTime:kCMTimeZero];CVPixelBufferRef buffer = NulL;buffer = [self pixelBufferFromCGImage:[first CGImage]];BOol result = [adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];if (result == NO) //failes on 3GS,but works on iphone 4 NSLog(@"Failed to append buffer");if(buffer) { CVBufferRelease(buffer);}//int reverseSort = NO;NSArray *newArray = array;int fps = 10;int i = 0;for (UIImage *image in newArray){ [NSThread sleepForTimeInterval:0.02]; if (adaptor.assetWriterinput.readyForMoreMediaData) { i++; CMTime frameTime = CMTimeMake(1,fps); CMTime lastTime = CMTimeMake(i,fps); CMTime presentTime = CMTimeAdd(lastTime,frameTime); UIImage *imgFrame = image;//[UIImage imageWithContentsOffile:filePath] ; buffer = [self pixelBufferFromCGImage:[imgFrame CGImage]]; BOol result = [adaptor appendPixelBuffer:buffer withPresentationTime:presentTime]; if (result == NO) //failes on 3GS,but works on iphone 4 { NSLog(@"Failed to append buffer"); NSLog(@"The error is %@",[vIDeoWriter error]); [NSThread sleepForTimeInterval:0.5]; } if(buffer) { CVBufferRelease(buffer); } } else { NSLog(@"error"); i--; }}//Finish the session:[writerinput markAsFinished];[vIDeoWriter finishWriting];CVPixelBufferPoolRelease(adaptor.pixelBufferPool);NSLog(@"MovIE created successfully");}- (CVPixelBufferRef) pixelBufferFromCGImage: (CGImageRef) image{ NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES],kCVPixelBufferCGImageCompatibilityKey,[NSNumber numberWithBool:YES],kCVPixelBufferCGBitmapContextCompatibilityKey,nil]; CVPixelBufferRef pxbuffer = NulL; CVPixelBufferCreate(kcfAllocatorDefault,CGImageGetWIDth(image),CGImageGetHeight(image),kCVPixelFormatType_32ARGB,(__brIDge CFDictionaryRef) options,&pxbuffer); CVPixelBufferLockBaseAddress(pxbuffer,0); voID *pxdata = CVPixelBufferGetBaseAddress(pxbuffer); CGcolorSpaceRef rgbcolorSpace = CGcolorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(pxdata,8,4*CGImageGetWIDth(image),rgbcolorSpace,kCGImageAlphaNoneskipFirst); CGContextConcatCTM(context,CGAffinetransformMakeRotation(0)); // CGAffinetransform flipVertical = CGAffinetransformMake( // 1,-1,CGImageGetHeight(image) // ); // CGContextConcatCTM(context,flipVertical); // CGAffinetransform flipHorizontal = CGAffinetransformMake( // -1.0,0.0,1.0,0.0 // ); // // CGContextConcatCTM(context,flipHorizontal); CGContextDrawImage(context,CGRectMake(0,CGImageGetHeight(image)),image); CGcolorSpaceRelease(rgbcolorSpace); CGContextRelease(context); CVPixelBufferUnlockBaseAddress(pxbuffer,0); return pxbuffer;}
但我遇到的问题是视频的输出有些损坏(尽管它有如下所示的有趣线条,但确实会播放)
我会非常感激任何帮助
非常感谢
托马斯
解决方法 我一直看到H264视频编码硬件存在问题,它可以破坏与已知宽高比不匹配的输入.例如,我的测试显示,如果一个视频维度小于128像素,则视频将不会编码.我所看到的工作是128×128,192×128,240×160,480×320等.
见这page on aspect ratios
附:您可能希望使用AVAssetWriterinputPixelBufferAdaptor,因为它包含可以通过CVPixelBufferPoolCreatePixelBuffer()使用的像素缓冲池.另外,你需要断言(adaptor.pixelBufferPool);在调用startSessionAtSourceTime之后,确保您的适配器可以写入writer.
总结以上是内存溢出为你收集整理的iphone – 视频错误的UIImage的NSArray在输出中有失真全部内容,希望文章能够帮你解决iphone – 视频错误的UIImage的NSArray在输出中有失真所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)