0

我有一个相当冗长的定格动画应用程序方法,对于按下的各种选项、定时器、自拍定时器等都略有不同

可以定义方法的主体:

 // initiate a still image capture, return immediately
// the completionHandler is called when a sample buffer has been captured
AVCaptureConnection *stillImageConnection = [stillImageOutput connectionWithMediaType:AVMediaTypeVideo];
[stillImageOutput captureStillImageAsynchronouslyFromConnection:stillImageConnection 
    completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *__strong error) {

      // set up the AVAssetWriter using the format description from the first sample buffer captured
      if ( !assetWriter ) {
          outputURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%llu.mov", NSTemporaryDirectory(), mach_absolute_time()]];
          //NSLog(@"Writing movie to \"%@\"", outputURL);
          CMFormatDescriptionRef formatDescription = CMSampleBufferGetFormatDescription(imageDataSampleBuffer);
          if ( NO == [self setupAssetWriterForURL:outputURL formatDescription:formatDescription] )
              return;
      }

      // re-time the sample buffer - in this sample frameDuration is set to 5 fps
      CMSampleTimingInfo timingInfo = kCMTimingInfoInvalid;
      timingInfo.duration = frameDuration;
      timingInfo.presentationTimeStamp = nextPTS;
      CMSampleBufferRef sbufWithNewTiming = NULL;
      OSStatus err = CMSampleBufferCreateCopyWithNewTiming(kCFAllocatorDefault, 
                                                           imageDataSampleBuffer, 
                                                           1, // numSampleTimingEntries
                                                           &timingInfo, 
                                                           &sbufWithNewTiming);
      if (err)
          return;

       // append the sample buffer if we can and increment presnetation time
      if ( [assetWriterInput isReadyForMoreMediaData] ) {
          if ([assetWriterInput appendSampleBuffer:sbufWithNewTiming]) {
              nextPTS = CMTimeAdd(frameDuration, nextPTS);
          }
          else {
              NSError *error = [assetWriter error];
              NSLog(@"failed to append sbuf: %@", error);
          }
      }

      // release the copy of the sample buffer we made
      CFRelease(sbufWithNewTiming);
    }];

只需使用计时器等对方法进行更改

首先,我尝试制作一个单例,但虽然我得到了名为的方法,但在保存和写入文件时遇到了其他问题。我可以用方法制作宏吗?

我在这里研究了 SO iOS 创建宏

我在正确的轨道上吗?我可以像在那个例子中那样定义一个方法而不是图像吗

4

1 回答 1

2

出于各种原因,尽管可能,从方法中制作宏是一个糟糕的主意。

为什么不把它变成一个类方法呢?您不必担心类实例的管理,也不会混淆全局命名空间。

于 2013-08-21T16:20:40.433 回答