从UIImages创建mov时的iOS绿色边框

从UIImages创建mov时的iOS绿色边框,第1张

概述我正在使用下面的代码来获取一系列UI Image并将它们转换为mov.出于某种原因,我继续在底部或右侧获得绿色边框,具体取决于它是横向照片还是纵向照片.图像是215×320. 如何删除绿色边框?是否有更好的方法从UIImages创建.mov? - (void)createMov{ UIImage *first = [self.videoFrames objectAtIndex:0]; 我正在使用下面的代码来获取一系列UI Image并将它们转换为mov.出于某种原因,我继续在底部或右侧获得绿色边框,具体取决于它是横向照片还是纵向照片.图像是215×320.

如何删除绿色边框?是否有更好的方法从UIImages创建.mov?

- (voID)createMov{  UIImage *first = [self.vIDeoFrames objectAtIndex:0];  CGSize frameSize = first.size;  NSError *error = nil;  AVAssetWriter *vIDeoWriter = [[AVAssetWriter alloc] initWithURL:                                [NSURL fileURLWithPath:filename] fileType:AVfileTypeQuickTimeMovIE                                                        error:&error];  if (error) {    NSLog(@"error creating AssetWriter: %@",[error description]);  }  int numPixels = first.size.wIDth * first.size.height;  int bitsPerSecond = numPixels * 11.04;  NSDictionary *vIDeoSettings = [NSDictionary dictionaryWithObjectsAndKeys:                                 AVVIDeoCodecH264,AVVIDeoCodecKey,[NSNumber numberWithInt:frameSize.wIDth],AVVIDeoWIDthKey,[NSNumber numberWithInt:frameSize.height],AVVIDeoHeightKey,[NSDictionary dictionaryWithObjectsAndKeys:                                  [NSNumber numberWithInteger:bitsPerSecond],AVVIDeoAverageBitRateKey,[NSNumber numberWithInteger:30],AVVIDeoMaxKeyFrameIntervalKey,nil],AVVIDeoCompressionPropertIEsKey,nil];  AVAssetWriterinput* writerinput = [[AVAssetWriterinput                                      assetWriterinputWithMediaType:AVMediaTypeVIDeo                                      outputSettings:vIDeoSettings] retain];  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 errors  writerinput.expectsMediaDataInRealTime = YES;  //Start a session:  BOol start = [vIDeoWriter startWriting];  NSLog(@"Session started? %d",start);  [vIDeoWriter startSessionAtSourceTime:kCMTimeZero];  // Writing.  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);  [NSThread sleepForTimeInterval:0.05];  int fps = [Utils frameRate];  int i = 0;  for (UIImage *imgFrame in self.vIDeoFrames) {    i = [self addFrame:adaptor vIDeoWriter:vIDeoWriter buffer:buffer imgFrame:imgFrame i:i fps:fps];  }  //Finish the session:  [writerinput markAsFinished];  [vIDeoWriter finishWriting];  CVPixelBufferPoolRelease(adaptor.pixelBufferPool);  [vIDeoWriter release];  [writerinput release];}- (int)addFrame:(AVAssetWriterinputPixelBufferAdaptor *)adaptor vIDeoWriter:(AVAssetWriter *)vIDeoWriter buffer:(CVPixelBufferRef)buffer imgFrame:(UIImage *)imgFrame i:(int)i fps:    (float)fps{  if (adaptor.assetWriterinput.readyForMoreMediaData) {    i++;    NSLog(@"insIDe for loop %d",i);    CMTime frameTime = CMTimeMake(1,fps);    CMTime lastTime=CMTimeMake(i,fps);    CMTime presentTime=CMTimeAdd(lastTime,frameTime);    buffer = [self pixelBufferFromCGImage:[imgFrame CGImage]];    BOol result = [adaptor appendPixelBuffer:buffer withPresentationTime:presentTime];    if (result == NO) { //fails on 3GS,but works on iphone 4      NSLog(@"Failed to append buffer");      NSLog(@"The error is %@",[vIDeoWriter error]);    }    if (buffer) {      CVBufferRelease(buffer);    }    [NSThread sleepForTimeInterval:0.05];  } else {    NSLog(@"error");    i--;  }  [NSThread sleepForTimeInterval:0.02];  return i;}- (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,(CFDictionaryRef) options,&pxbuffer);  CVPixelBufferLockBaseAddress(pxbuffer,0);  voID *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);  size_t rowBytes = CVPixelBufferGetBytesPerRow(pxbuffer);  CGcolorSpaceRef rgbcolorSpace = CGcolorSpaceCreateDeviceRGB();  CGContextRef context = CGBitmapContextCreate(pxdata,8,rowBytes,rgbcolorSpace,kCGImage@R_502_5997@NoneskipFirst);  CGContextConcatCTM(context,CGAffinetransformMakeRotation(0));  CGContextDrawImage(context,CGRectMake(0,CGImageGetHeight(image)),image);  CGcolorSpaceRelease(rgbcolorSpace);  CGContextRelease(context);  CVPixelBufferUnlockBaseAddress(pxbuffer,0);  return pxbuffer;}
解决方法 我似乎得到了类似的问题,在阅读了两篇文章之后,我从头脑中得到了一个想法.

http://forum.videohelp.com/threads/314973-green-line-at-bottom-of-video-window

http://en.wikipedia.org/wiki/H.264/MPEG-4_AVC#Features

NSDictionary *vIDeoSettings = [NSDictionary dictionaryWithObjectsAndKeys:                                       AVVIDeoCodecH264,[NSNumber numberWithUnsignedLong:sourceWIDth],[NSNumber numberWithUnsignedLong:sourceHeight],nil];AVAssetWriterinput *writerinput =        [AVAssetWriterinput assetWriterinputWithMediaType:AVMediaTypeVIDeo outputSettings:vIDeoSettings];

在开始通过AVAssertWriter写入数据之前,请使用以下方法重置sourceWIDth和sourceHeight:

size_t fixedlineValue(size_t sourceValue){    size_t divIDe = sourceValue%4;    size_t finalValue = sourceValue;    if (divIDe) {        finalValue=(sourceValue/4+1)*4;    }    return finalValue;}

我不清楚为什么这样做,但在那之后,丑陋的绿色边框被删除了.请评论是否有更好的答案,让我们知道.谢谢.

总结

以上是内存溢出为你收集整理的从UIImages创建mov时的iOS绿色边框全部内容,希望文章能够帮你解决从UIImages创建mov时的iOS绿色边框所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1036062.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存