首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

使用 Watson 和 IoT Platform 服务构建家庭助理移动应用程序(10)

使用 Watson 和 IoT Platform 服务构建家庭助理移动应用程序(10)

利用 Object Storage 服务检索图像要利用 Object Storage 服务检索图像,必须准备好您的 Xcode 项目,然后初始化它。
准备 Xcode 项目
  • 将以下代码添加到 Podfile 来安装             框架。
    1
    pod 'BluemixObjectStorage'




  • 运行下面的命令来安装 BluemixObjectStorage          依赖项。
    1
    pod install




初始化
  • 创建一个名为 ObjectStorageExt.swift 的文件,以包含连接 Object Storage 服务的代码并为            ViewController            声明一个扩展。
    1
    2
    extension ViewController {
    }




  • 使用之前保存的 [OS_PROJECTID]、[OS_USERNAME] 和            [OS_PASSWORD]            初始化该服务。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    self.objectStorage = ObjectStorage(projectId: Credentials.ObjectStorageProjectId)
        objectStorage.connect(userId: Credentials.ObjectStorageUserId,
                           password: Credentials.ObjectStoragePassword,
                           region: ObjectStorage.Region.Dallas) {
                              error in
                              if let error = error {
                                print("objectstorage connect error :: \(error)")
                              } else {
                                print("objectstorage connect success")
                              }
        }




  • 实现 downloadPictureFromObjectStorage() 函数来下载 Node-RED            应用程序拍摄并上传的照片。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    func downloadPictureFromObjectStorage(containername: String, objectname: String) {
        self.objectStorage.retrieve(container: containername) {
          error, container in
          if let error = error {
            print("retrieve container error :: \(error)")
          } else if let container = container {
            container.retrieve(object: objectname) {
              error, object in
              if let error = error {
                print("retrieve object error :: \(error)")
              } else if let object = object {
                print("retrieve object success :: \(object.name)")
                guard let data = object.data else {
                  return
                }
                if let image = UIImage(data: data) {
                  self.addPicture(image)
                  self.didReceiveConversationResponse(["Picture taken"])
                }
              } else {
                print("retrieve object exception")
              }
            }
          } else {
            print("retrieve container exception")
          }
        }
      }




结束语本教程介绍了一个移动应用程序如何使用 Watson Conversation、Text to Speech 和 Speech to Text 服务来理解用户命令。然后通过        Watson IoT Platform 服务使用这些破译的命令来控制设备。教程中还解释了如何集成 Raspberry Pi        作为家庭网关,从移动应用程序接收命令并向其发送事件。最后,介绍了如何使用 Object Storage 服务存储图像。
返回列表