【发布时间】:2018-06-05 18:24:19
【问题描述】:
我正在使用 iOS 11.4 和 Swift 4 构建一个 iOS 应用程序,该应用程序假设在 iOS 屏幕上显示本地图像库。当我从图库中选择一张图片时,我想通过 Chromecast 将该图片发送到电视屏幕。 我可以连接到我的 Chromecast 设备,但是当我尝试发送图像时,它只在电视屏幕上显示投射图标。这是将图像传输到 Chromecast 的代码:
PHImageManager.default().requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFit, options: options, resultHandler: { image, _ in
// If successful, show the image view and display the image.
guard let image = image else { return }
// Now that we have the image, show it.
imageview.isHidden = false
// Here the image is correctly displayed on the screen
imageview.image = image
// Checking if cast session started
if self.sessionManager.hasConnectedSession() == true {
// Creating image name to save it as URL
var imageName = "Image_"
// Give image unique name
imageName = imageName + "\(self.imageNameInt)"
self.imageNameInt += 1
// Saving image to local URL so it will be compatible for use
// in GCKMediaInformation
let imageData = UIImageJPEGRepresentation(imageview.image!, 0.8)
let docDir = try! FileManager.default.url(for: .documentDirectory,
in: .userDomainMask, appropriateFor: nil, create: true)
let imageURL = docDir.appendingPathComponent(imageName)
try! imageData?.write(to: imageURL)
let metadata = GCKMediaMetadata(metadataType: .photo)
let imageAsset = imageURL.absoluteString
self.mediaInfo = GCKMediaInformation(
contentID: imageAsset,
streamType: .buffered,
contentType: "image/jpg",
metadata: metadata,
streamDuration: 0,
mediaTracks: nil,
textTrackStyle: nil,
customData: nil)
self.sessionManager.currentCastSession?.remoteMediaClient?.loadMedia(self.mediaInfo!)
}
})
每次调用 loadMedia 都会刷新电视屏幕,但唯一显示的是投射图标。有人可以查看这段代码,看看是否有问题导致图像无法发送到电视屏幕?
【问题讨论】:
-
你能把
contentType: "image/jpeg"和streamType:.none换一下吗?
标签: ios chromecast