从UIImages创建mov时iOS显示绿色边框

4
我正在使用下面的代码将一系列UIImage转换为mov。不知何故,无论是横向还是纵向照片,底部或右侧都会出现绿色边框。这些图片的尺寸为215x320。
如何去除绿色边框?有没有更好的方法从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, CGImageGetWidth(image),
                                           CGImageGetHeight(image), 8, rowBytes, rgbColorSpace,
                                           kCGImageAlphaNoneSkipFirst);

  CGContextConcatCTM(context, CGAffineTransformMakeRotation(0));


  CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),
                                     CGImageGetHeight(image)), image);
  CGColorSpaceRelease(rgbColorSpace);
  CGContextRelease(context);

  CVPixelBufferUnlockBaseAddress(pxbuffer, 0);

  return pxbuffer;
}

以下是如何注释掉下面的属性?[NSNumber numberWithInteger:bitsPerSecond],AVVideoAverageBitRateKey, - alones
1个回答

2
似乎我遇到了类似的问题,在阅读以下两篇文章后,我突然想到了一个主意。

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, AVVideoCodecKey,
                                       [NSNumber numberWithUnsignedLong:sourceWidth], AVVideoWidthKey,
                                       [NSNumber numberWithUnsignedLong:sourceHeight], AVVideoHeightKey,
                                       nil];
AVAssetWriterInput *writerInput =
        [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];

在使用AVAssertWriter写入数据之前,请使用此方法重置sourceWidthsourceHeight
size_t fixedLineValue(size_t sourceValue){
    size_t divide = sourceValue%4;
    size_t finalValue = sourceValue;
    if (divide) {
        finalValue=(sourceValue/4+1)*4;
    }
    return finalValue;
}   

我无法清楚地理解为什么会这样,但之后,丑陋的绿色边框被移除了。如果有更好的答案,请评论并告知我们。谢谢。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接