【问题标题】:How to define a constant method如何定义一个常量方法
【发布时间】:2013-08-21 16:12:25
【问题描述】:

我有一个相当冗长的定格动画应用方法,它在按下的各种选项、计时器、自拍等方面略有不同

可以定义方法的主体:

 // 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 create macro

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

【问题讨论】:

  • 您可能想要创建一个包含此方法的实用程序类,您可以创建该类的一个实例以进行实际处理。如果您不想实例化一个类,您可以使用类方法创建一个类(例如+ (void)doSomeThing)并直接使用它们。

标签: ios objective-c cocoa-touch


【解决方案1】:

虽然可以从方法中创建宏,但由于各种原因,这是一个糟糕的主意。

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

【讨论】:

  • OK 点重新宏。所以如果我在 .h 中声明我的方法为 + (void)myMethod;我如何/在哪里放置这个 in.m 以进行检索我检查了这个 stackoverflow.com/questions/1053592/…
  • 在实现块中,就像一个实例方法。
  • 那我不明白类方法的概念。你有例子吗?
猜你喜欢
  • 1970-01-01
  • 2012-02-19
  • 2015-08-11
  • 2019-02-06
  • 2010-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-08
相关资源
最近更新 更多