title: Convert from CMSampleBuffer to NSData image without exceeding 50MB of memory
body: <p>Error exceeding memory allowance when processing CMSampleBuffer to NSData conversion in RPBroadcastSampleHandler.
  Error code: Thread 7: EXC_RESOURCE RESOURCE_TYPE_MEMORY (limit = 50 MB, unused = 0x0)</p>
  
  <p>I am processing the function of converting a large 1920x1440 image into a binary to send to the socket server. 
  I tried resizing to 1200x900 but nothing worked.</p>
  
  <p>I have spent a week dealing with this problem, but to no avail.
  I have tried the following:
  Resize image with instructions from here: <a href="https://nshipster.com/image-resizing/" rel="nofollow noreferrer">https://nshipster.com/image-resizing/</a></p>
  
  <ol>
  <li>Get the UIImage object from SampleBuffer and then convert it to jpegData with the lowest compression quality.</li>
  </ol>
  
  <pre><code>    func getDataFromCMSampleBuffer (sampleBuffer: CMSampleBuffer) -&gt; Data? {
          if CMSampleBufferDataIsReady (sampleBuffer),
              let pixelBuffer = CMSampleBufferGetImageBuffer (sampleBuffer) {
              let ciImage = CIImage (cvImageBuffer: pixelBuffer)
              let image = UIImage (ciImage: ciImage)
              return (image.jpegData (compressionQuality: 0.0)) // Error Thread 7: EXC_RESOURCE RESOURCE_TYPE_MEMORY (limit = 50 MB, unused = 0x0)
          }
          return nil
      }
  </code></pre>
  
  <ol start="2">
  <li>Resize the UIImage object from CMSampleBuffer before convert to NSData</li>
  </ol>
  
  <pre><code>    let renderer = UIGraphicsImageRenderer (size: CGSize (width: 1200, height: 900))
      return renderer.image {(context) in
          image.draw (in: CGRect (origin: .zero, size: size)) // Error Thread 7: EXC_RESOURCE RESOURCE_TYPE_MEMORY (limit = 50 MB, unused = 0x0)
      }
  </code></pre>
  
  <p>Or</p>
  
  <pre><code>    UIGraphicsBeginImageContextWithOptions (CGSizeMake (newWidth, newHeight), NO, 0);
      [sourceImage drawInRect: CGRectMake (0, 0, 1200, 900)]; //// Error Thread 7: EXC_RESOURCE RESOURCE_TYPE_MEMORY (limit = 50 MB, unused = 0x0)
      UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext ();
      UIGraphicsEndImageContext ();
  </code></pre>
  
  <ol start="3">
  <li>Get the Image bitmap from CMSampleBuffer before convert to NSData</li>
  </ol>
  
  <pre><code>    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
      CGContextRef context = CGBitmapContextCreate (rgbBuffer, width, height, 8, width * bytesPerPixel, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
      CGImageRef quartzImage = CGBitmapContextCreateImage (context); // Thread 7 error: EXC_RESOURCE RESOURCE_TYPE_MEMORY (limit = 50 MB, unused = 0x0)
      UIImage * image = [UIImage imageWithCGImage: quartzImage];
  </code></pre>
  
  <ol start="4">
  <li>Scale CIImage before convert to NSData </li>
  </ol>
  
  <pre><code>    CIFilter *filter = [CIFilter filterWithName:@"CILanczosScaleTransform"];
      [filter setValue:image forKey:kCIInputImageKey];
      [filter setValue:[NSNumber numberWithDouble:0.2] forKey:kCIInputScaleKey];
      [filter setValue:@1.0 forKey:kCIInputAspectRatioKey];
      CIImage *img = (CIImage*)[filter valueForKey:kCIOutputImageKey];
  
  ...
      CIContext *context = [CIContext contextWithOptions:@{kCIContextUseSoftwareRenderer: @NO}];
  
      CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
  
      [context render:img toBitmap:rawData rowBytes:width*kComponentsPerPixel bounds:img.extent format:kCIFormatRGBA8 colorSpace:rgb];// Thread 7 error: EXC_RESOURCE RESOURCE_TYPE_MEMORY (limit = 50 MB, unused = 0x0)
  
      CGDataProviderRef provider = CGDataProviderCreateWithData(nil, rawData, memsize, releasePixels);
  </code></pre>
  
  <p>Is there a way to convert from CMSampleBuffer to NSData image without exceeding 50MB of memory and the image quality is not reduced too much.
  Or is there any way to resize large images that don't use more than 50MB of memory?</p>
  
  <p>Any advice I give is very thankful.</p>