现在的位置: 首页 > 综合 > 正文

How to Convert AVFrame from NSImage or UIImage

2013年11月06日 ⁄ 综合 ⁄ 共 883字 ⁄ 字号 评论关闭
AVFrame *frame = avcodec_alloc_frame();
      int numBytes = avpicture_get_size(PIX_FMT_YUV420P, outputWidth, outputHeight);
      uint8_t *buffer = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
      avpicture_fill((AVPicture *)frame, buffer, PIX_FMT_YUV420P, outputWidth, outputHeight);
      //UIImage *image = ? smothing ? ;
      NSImage *image = ? smothing ? ;
      //CGImageRef newCgImage = image.CGImage;
      CGImageRef newCgImage = [image CGImageForProposedRect:nil context:nil hints:nil];
      //NSBitmapImageRep* bm = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];
      //CGImageRef newCgImage = [bm CGImage];
      size_t w = CGImageGetWidth(newCgImage);
      size_t h = CGImageGetHeight(cgImage);
      CGDataProviderRef dataProvider = CGImageGetDataProvider(newCgImage);
      CFDataRef bitmapData = CGDataProviderCopyData(dataProvider);
      uint8_t *buffer = (uint8_t *)CFDataGetBytePtr(bitmapData);
      frame->linesize[0] = w;
      int y, x;
      for (y = 0; y < h; y++) {
        for (x = 0; x < w; x++) {
          int z = y * w + x;
          frame->data[0][z] = buffer[z];
        }
      }

抱歉!评论已关闭.