通过 bonjour 在两个 iOS 设备之间流式传输图像

2024-01-15

我的目标是通过 bonjour 将 AVCaptureOutput 捕获的图像从一台 iOS 设备流式传输到另一台设备。

这是我当前的方法:

1) 从视频输入中捕获帧

- (void)captureOutput:(AVCaptureOutput *)captureOutput 
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
   fromConnection:(AVCaptureConnection *)connection 
{ 
    /*code to convert sampleBuffer into UIImage */
    NSData * imageData = UIImageJPEGRepresentation(image,1.0);
    [connection sendImage:image];
}

2)通过TCP连接发送(来自http://mobileorchard.com/tutorial-networking-and-bonjour-on-iphone/ http://mobileorchard.com/tutorial-networking-and-bonjour-on-iphone/)

// Send raw image over network
- (void)sendRawImagePacket:(UIImage *)image {
// Encode packet
NSData * imageData = UIImageJPEGRepresentation(image, 1.0);

NSData * rawPacket = [NSKeyedArchiver archivedDataWithRootObject:imageData];

// Write header: length of raw packet
int packetLength = [rawPacket length];

[outgoingDataBuffer appendBytes:&packetLength length:sizeof(int)];

[outgoingDataBuffer appendData:rawPacket];

// Try to write to stream
[self writeOutgoingBufferToStream];
}

3)读取数据并将其转换回图像以显示在接收设备的UIImageView上

// Read as many bytes from the stream as possible and try to extract meaningful packets
- (void)readFromStreamIntoIncomingBuffer {
// Temporary buffer to read data into
UInt8 buf[1024];

// Try reading while there is data
while( CFReadStreamHasBytesAvailable(readStream) ) {  
   CFIndex len = CFReadStreamRead(readStream, buf, sizeof(buf));
if ( len <= 0 ) {
  // Either stream was closed or error occurred. Close everything up and treat this as "connection terminated"
  [self close];
  [delegate connectionTerminated:self];
  return;
 }

  [incomingDataBuffer appendBytes:buf length:len];
}

// Try to extract packets from the buffer.
//
// Protocol: header + body
//  header: an integer that indicates length of the body
//  body: bytes that represent encoded NSDictionary

// We might have more than one message in the buffer - that's why we'll be reading it inside the while loop
 while( YES ) {
// Did we read the header yet?
if ( packetBodySize == -1 ) {
  // Do we have enough bytes in the buffer to read the header?
  if ( [incomingDataBuffer length] >= sizeof(int) ) {
    // extract length
    memcpy(&packetBodySize, [incomingDataBuffer bytes], sizeof(int));

    // remove that chunk from buffer
    NSRange rangeToDelete = {0, sizeof(int)};
    [incomingDataBuffer replaceBytesInRange:rangeToDelete withBytes:NULL length:0];
  }
  else {
    // We don't have enough yet. Will wait for more data.

      break;

  }
}

// We should now have the header. Time to extract the body.
if ( [incomingDataBuffer length] >= packetBodySize ) {
  // We now have enough data to extract a meaningful packet.
  NSData* raw = [NSData dataWithBytes:[incomingDataBuffer bytes] length:packetBodySize];

  // Tell our delegate about it

            NSData * imageData = [NSKeyedUnarchiver unarchiveObjectWithData:raw];

            UIImage * image = [UIImage imageWithData:imageData];
            [delegate receivedNetworkRawImage:image viaConnection:self];


  // Remove that chunk from buffer
  NSRange rangeToDelete = {0, packetBodySize};
  [incomingDataBuffer replaceBytesInRange:rangeToDelete withBytes:NULL length:0];

  // We have processed the packet. Resetting the state.
  packetBodySize = -1;
}
else {
  // Not enough data yet. Will wait.
  break;
 }
}
}

但是,当连接不稳定时,UIImage 会抛出无法渲染 JPEG 的错误。

我应该如何通过wifi传递图像?

如果跳过一些帧也没关系,我需要一种方法来告诉 UIImage 跳过那“一批”坏数据。

Thanks!


UIImage 不符合 NSCoding --> NSKeyedArchiver 失败。

您必须使用 UIImagePNGRepresentation() 来获取图像的数据。或者使用 UIImageJPEGRepresentation() 来压缩数据。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过 bonjour 在两个 iOS 设备之间流式传输图像 的相关文章

  • 获取所有ios应用程序的全局列表[重复]

    这个问题在这里已经有答案了 我想对苹果应用商店进行一些全球统计 一个瓶颈是至少获取所有当前活动应用程序的 ID 这 9 位数字 有谁知道如何获取 iOS 应用商店中当前活动应用程序的所有 id 的完整列表 更好的是特定类别的所有 ID 例如
  • 带有自定义字体的 UILabel 错误呈现

    在我的 iPhone 应用程序中 我为所有 UILabel 设置了自定义字体 更准确地说 我对 UILabel 进行了子类化 重写了一个方法 在该方法中设置了自定义字体 然后将 IB 中的所有标签设置为该自定义类 现在的问题是 所有文本都渲
  • iOS 中是否需要 Google App Indexing SDK 才能使用 Google DeepLinking?

    我想用谷歌应用程序索引与我的网页和 iOS 应用程序 我支持通用链接 or 深层链接用谷歌术语 与苹果Search并相应地设置我的网页 From 谷歌文档 https developers google com app indexing i
  • 聊天室成员列表

    如何检索正在使用的聊天室的成员列表XMPP framework 我尝试使用 void xmppRoom XMPPRoom sender didFetchMembersList NSArray items 但它返回一个空数组 这个问题很老了
  • iOS 7 UITableView:这是一个错误还是我的问题?

    请参阅附图 在 iOS 7 的表格视图中 UIKit 在附件视图和重新排序控件之间绘制一条细灰色垂直线 但是 当滚动表视图时 某些单元格中不会绘制这条线 附图中的单元格 1 2 和 8 中不存在该值 为什么是这样 我该如何修复它 我在 ip
  • 您必须提供 5.5 英寸 Retina 显示屏的屏幕截图(适用于 4.7 英寸)

    当我按下提交审核按钮进入iTunes连接 我收到错误 您必须提供 5 5 英寸 Retina 显示屏的屏幕截图 因为您的应用程序二进制文件支持 5 5 英寸 Retina 显示屏 这是因为我添加了启动图像适用于 5 5 和 4 7 显示器
  • FB SDK 3.0 我是否需要扩展访问令牌还是自动的?

    基于http developers facebook com roadmap offline access removal http developers facebook com roadmap offline access remova
  • Alamofire 仅在 GET 请求上出现请求错误

    我正在努力将我的项目从 AFNetworking 转移到 Alamofire 真的很喜欢这个项目 POST 请求工作得很好 但是 我在尝试发出 GET 请求时收到此错误 这是一些示例代码 class func listCloudCrednt
  • 使用 CLPlacemark、administrativeArea、iOS6/iOS7 更改内容

    我计划为 ios 7 制作一个应用程序 并且有管理区域地标属性的问题 对于 iOS6 我得到行政区域的全名 例如 加利福尼亚 但对于 iOS7 我得到 CA 的值 当情况如此变化时 这是一个问题 有什么方法可以控制这个输入 使其更加一致吗
  • 我可以在滚动时固定表格的 tableHeaderView 位置吗?

    我有一个表视图 并在其 tableHeaderView 上附加了一个 UISegmentedControl 如何使 tableHeaderView 固定 以便即使在滚动表视图时也始终可以在同一位置查看 UISegmentedControl
  • 如何更改某些功能以兼容 iOS 10 或更低版本的 Snapchat 中的某些功能,例如相机视图控制器

    我正在制作一个视图控制器来制作像 snapchat 相机一样的相机视图控制器 我下面的代码在 iOS 11 或更高版本上完美运行 老实说 我并没有真正掌握我的代码 因为我只是按照这个像相机视图控制器这样的 snapchat 的教程进行操作
  • 从钥匙串保存和加载 |斯威夫特[重复]

    这个问题在这里已经有答案了 如何简单地将字符串存储在钥匙串中并在需要时加载 有几种SO解决方案 主要参考Git repo 但我需要最新 Swift 上最小和最简单的解决方案 当然 我不想添加 git 框架来简单地在我的项目中存储密码 有类似
  • 将 NSDictionary 保存到文件有任何限制吗

    我想用下面的方法来保存NSDictionary void writeDicToFile NSDictionary dic fileName NSString fileName NSString filePath NSTemporaryDir
  • TypeError:cli.init 不是 React Native 的函数

    在 MacBook Air M1 芯片中运行 npx react native init appName 时 TypeError cli init is not a function at run opt homebrew lib node
  • Xcode 和 Waze 集成

    我正在尝试整合我的app with waze http www waze com 有人知道如何调用位智并发送坐标吗 我没有找到任何 API 或其他相关信息 void navigateToLatitude double latitude lo
  • 如何在调用-reloadData后保留UITableView contentoffset

    CGPoint offset table contentOffset table reloadData table setContentOffset offset animated NO unuseful block UITableView
  • Quickblox 聊天未进行身份验证

    我在我的应用程序中使用 Quickblox 进行一对一聊天 用户已经登录 但是当我尝试登录聊天时 出现以下错误 2014 03 31 12 42 09 532 MyChat 2175 3803 QBChat didNotAuthentica
  • Base64Transcoder.m 重复符号

    我想使用 SKPSMTPMessage 库 唯一的问题是这个库包含文件 Base64Transcoder m 由于我有 Dropbox SDK 该文件会出现重复错误 我该如何解决这个错误 我不能直接删除 Base64Transcoder m
  • 奇怪的 Facebook ID [关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 我有两个 Facebook 应用程序 它们都正在开发中 其中一个是很久以前创建的 而另一个则是相当新的 自从我升级到新的 iOS Faceb
  • Eddystone 信标检测问题

    以下是我使用 iPhone iOS 9 检测 Eddystone 的代码 void viewDidLoad super viewDidLoad if CLLocationManager locationServicesEnabled loc

随机推荐