0

I have a fairly lengthy method for a stop motion app that is slightly different for each of the various options pressed, timers, self timers, etc

Can define the main body of the method:

 // 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);
    }];

and just make variations of the method with the timers etc

First I tried making a singleton but although I got the method called I had other issues with the saving and writing to file. Can I make a MACRO out of a method?

I researched on SO here iOS create macro

Am I on the right track? can i define a method rather than image as in that example

Community
  • 1
  • 1
JSA986
  • 5,870
  • 9
  • 45
  • 91
  • You may want to create a utility class that contains this method, you could create an instance of the class in order to do the actual processing. If you don't want to instantiate a class you could create a class with class methods (e.g. `+ (void)doSomeThing`) and use those directly. – jessecurry Aug 21 '13 at 16:28

1 Answers1

2

Making a macro out of a method, while possible, is a terrible idea for a variety of reasons.

Why not just make it a class method? You won't have to worry about management of a class instance, and it won't muddy up the global namespace.

Jeff
  • 4,751
  • 5
  • 31
  • 35
  • Ok point taken re the MACRO. SO if I declare my method in .h as + (void)myMethod; How/where do I place this in.m for retrieval I checked out this http://stackoverflow.com/questions/1053592/what-is-the-difference-between-class-and-instance-methods – JSA986 Aug 21 '13 at 16:26
  • In the implementation block, just like an instance method. – Jeff Aug 21 '13 at 16:27
  • I dont understand the concept of a class method then. Have you any examples? – JSA986 Aug 21 '13 at 16:37