title: CVPixelBuffer Writing using UIImage.draw() is TOO SLOW
body: <p>I'm an undergraduate student and I'm now using CoreML frame to do some video HumanSeg app on iPhone, but as the title shows, I've got a huuuuuge problem. </p>
  
  <p>I have a UIImage and I have to resize and pad it, and draw it into a CVPixelBuffer to feed the MobileNet model, but such process is just TOO SLOW, costing about 30ms, which is unacceptable.</p>
  
  <p>To be specific, In my code, method UIImage.draw(in: CGRect(x: Int, y: Int, width: Int, height: Int)) is TOO SLOW, and took me 20+ ms, which is the major issue.</p>
  
  <p>My codes are below:</p>
  
  <pre><code>func dealRawImage(image : UIImage, dstshape : [Int], pad : UIImage) -&gt; CVPixelBuffer?
  {
      // decide whether to shrink in height or width
      let height = image.size.height
      let width = image.size.width
      let ratio = width / height
      let dst_width = Int(min(CGFloat(dstshape[1]) * ratio, CGFloat(dstshape[0])))
      let dst_height = Int(min(CGFloat(dstshape[0]) / ratio, CGFloat(dstshape[1])))
      let origin = [Int((dstshape[0] - dst_height) / 2), Int((dstshape[1] - dst_width) / 2)]
  
      // init a pixelBuffer to store the resized &amp; padded image
      var pixelBuffer: CVPixelBuffer?
      let attrs = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue,
                   kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue]
      CVPixelBufferCreate(kCFAllocatorDefault,
                          dstshape[1],
                          dstshape[0],
                          kCVPixelFormatType_32ARGB,
                          attrs as CFDictionary,
                          &amp;pixelBuffer)
  
      // get the pointer of this pixelBuffer
      CVPixelBufferLockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
      let pixelData = CVPixelBufferGetBaseAddress(pixelBuffer!)
  
      // init a context that contains this pixelBuffer to draw in
      let context = CGContext(data: pixelData,
                              width: dstshape[1],
                              height: dstshape[0],
                              bitsPerComponent: 8,
                              bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer!),
                              space: CGColorSpaceCreateDeviceRGB(),
                              bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue)!
  
      // push context
      UIGraphicsPushContext(context)
      context.translateBy(x: 0, y: CGFloat(dstshape[0]))
      context.scaleBy(x: 1, y: -1)
  
      pad.draw(in:CGRect(x: 0, y: 0, width: dstshape[1], height: dstshape[0]))
      // THIS SINGLE FUNCTION COSTS ME 20+ ms AND IS THE MAJOR ISSUE !
      image.draw(in: CGRect(x: origin[1], y: origin[0], width: dst_width, height: dst_height))
  
      UIGraphicsPopContext()
  
      // unlock
      CVPixelBufferUnlockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
  
      return pixelBuffer
  }
  </code></pre>
  
  <p>And I just call this function like this: </p>
  
  <pre><code>let input = dealRawImage(image: raw_input_image, dstshape: [224, 224], pad: black_image)
  </code></pre>
  
  <p>Where raw_input_image is the UIImage I read from memory, dstshape is the shape I want to resize this image to, and black_image is a totally black UIImage used for padding.</p>
  
  <p>I've searched on this website but no familiar issue was found.</p>
  
  <p>Is there any way I can make this process faster and save this project? I just don't want to abandon my 2-week work. </p>