AppleTv / TvOS - 调整保存到 ImageURL 的 TVContentItems 中 TopShelf 图像的大小
AppleTv / TvOS - Resize TopShelf Images in TVContentItems Saved to ImageURL
我正在 Apple tvOS 中实现 TopShelf。我已下载图像并分配给 TVContentItems 的 imageURL。下载的图像纵横比不适合 TopShelf 图像。我试图通过将宽度 + 高度附加到图像来更改大小 link。
www.mydownloadedimages.com/{宽度}x{高度}
但是没有用。
我可以用任何其他方式在客户端调整大小吗?在 TVContentItem class 我只有 NSURL 对象。没有 UIImage 对象。
非常感谢。
这是 Apple 关于图像大小和形状的文档
// ~ 2 : 3
// ~ 1 : 1
// ~ 4 : 3
// ~ 16 : 9
// ~ 8 : 3
// ~ 87 : 28
//@property imageShape
//@abstract A TVContentItemImageShape value describing the intended aspect ratio or shape of the image.
//@discussion For Top Shelf purposes: the subset of values which are //valid in this property, for TVContentItems in the topShelfItems property //of the TVTopShelfProvider, depends on the value of the topShelfStyle //property of the TVTopShelfProvider:
TVTopShelfContentStyleInset:
valid: TVContentItemImageShapeExtraWide
TVTopShelfContentStyleSectioned:
valid: TVContentItemImageShapePoster
valid: TVContentItemImageShapeSquare
valid: TVContentItemImageShapeHDTV
当此属性的值对Top Shelf样式无效时,系统保留以任何方式缩放图像的权利。
你说得对 TVContentItem
没有 UIImage
类型 属性。由于 TVContentItem
也接受 imageURL
属性 中的本地文件 URL,解决方法可以是:
- 正在从互联网上抓取
UIImage
- 创建一个新的图像上下文,其大小与货架顶层图像相同
- 保存到
NSCacheDirectory
- 设置本地图片URL为
imageURL
。
步骤如下:
让我们创建我们的 TVContentItem
object:
let identifier = TVContentIdentifier(identifier: "myPicture", container: wrapperID)!
let contentItem = TVContentItem(contentIdentifier: identifier )!
设置contentItem
的imageShape
:
contentItem.imageShape = .HDTV
从网上抓取图片。实际上我是同步完成的,你也可以尝试使用其他异步方法来获取它(NSURLConnection
、AFNetworking 等...):
let data : NSData = NSData(contentsOfURL: NSURL(string: "https://s3-ak.buzzfed.com/static/2014-07/16/9/enhanced/webdr08/edit-14118-1405517808-7.jpg")!)!
准备您的图像将被保存的路径,并从 data
object:
中获取您的 UIImage
let filename = "picture-test.jpg"
let paths = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)
let filepath = paths.first! + "/" + filename
let img : UIImage = UIImage(data: data)!
假设您已经设置了 topShelfStyle
属性,使用方法 TVTopShelfImageSizeForShape
获取顶部货架图像的大小。这将是您的图像上下文的大小:
let shapeSize : CGSize = TVTopShelfImageSizeForShape(contentItem.imageShape, self.topShelfStyle)
创建 shapeSize
大小的图像上下文并将下载的图像绘制到上下文矩形中。您可以在此处对图像进行所有修改,以将其调整为所需的大小。在此示例中,我从 Instagram 获取了一张方形图像,并在右侧和左侧放置了白色信箱带。
UIGraphicsBeginImageContext(shapeSize)
let imageShapeInRect : CGRect = CGRectMake((shapeSize.width-shapeSize.height)/2,0,shapeSize.height,shapeSize.height)
img.drawInRect(imageShapeInRect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
最后把这张图片存入你的NSCacheDirectory
,图片路径设置为contentItem
的imageURL
。
UIImageJPEGRepresentation(newImage, 0.8)!.writeToFile(filepath, atomically: true)
contentItem.imageURL = NSURL(fileURLWithPath: filepath)
我想补充 Nicola Giancecchi 的回答。 Top Shelf 扩展将在非主线程上 运行,因此使用 UIImageJPEGRepresentation
或 UIImagePNGRepresentation
等方法有时会停止 Top Shelf 线程在控制台中打印此内容:
Program ended with exit code: 0
要解决这个问题,您可以像这样包装您的代码:
DispatchQueue.main.sync {
UIImageJPEGRepresentation(newImage, 0.8)!.writeToFile(filepath, atomically: true)
let imageURL = NSURL(fileURLWithPath: filepath)
if #available(tvOSApplicationExtension 11.0, *) {
contentItem.setImageURL(imageURL, forTraits: .userInterfaceStyleLight)
contentItem.setImageURL(imageURL, forTraits: .userInterfaceStyleDark)
} else {
contentItem.imageURL = imageURL
}
}
我正在 Apple tvOS 中实现 TopShelf。我已下载图像并分配给 TVContentItems 的 imageURL。下载的图像纵横比不适合 TopShelf 图像。我试图通过将宽度 + 高度附加到图像来更改大小 link。
www.mydownloadedimages.com/{宽度}x{高度}
但是没有用。
我可以用任何其他方式在客户端调整大小吗?在 TVContentItem class 我只有 NSURL 对象。没有 UIImage 对象。
非常感谢。
这是 Apple 关于图像大小和形状的文档
// ~ 2 : 3
// ~ 1 : 1
// ~ 4 : 3
// ~ 16 : 9
// ~ 8 : 3
// ~ 87 : 28
//@property imageShape
//@abstract A TVContentItemImageShape value describing the intended aspect ratio or shape of the image.
//@discussion For Top Shelf purposes: the subset of values which are //valid in this property, for TVContentItems in the topShelfItems property //of the TVTopShelfProvider, depends on the value of the topShelfStyle //property of the TVTopShelfProvider:
TVTopShelfContentStyleInset:
valid: TVContentItemImageShapeExtraWide
TVTopShelfContentStyleSectioned:
valid: TVContentItemImageShapePoster
valid: TVContentItemImageShapeSquare
valid: TVContentItemImageShapeHDTV
当此属性的值对Top Shelf样式无效时,系统保留以任何方式缩放图像的权利。
你说得对 TVContentItem
没有 UIImage
类型 属性。由于 TVContentItem
也接受 imageURL
属性 中的本地文件 URL,解决方法可以是:
- 正在从互联网上抓取
UIImage
- 创建一个新的图像上下文,其大小与货架顶层图像相同
- 保存到
NSCacheDirectory
- 设置本地图片URL为
imageURL
。
步骤如下:
让我们创建我们的
TVContentItem
object:let identifier = TVContentIdentifier(identifier: "myPicture", container: wrapperID)! let contentItem = TVContentItem(contentIdentifier: identifier )!
设置
contentItem
的imageShape
:contentItem.imageShape = .HDTV
从网上抓取图片。实际上我是同步完成的,你也可以尝试使用其他异步方法来获取它(
NSURLConnection
、AFNetworking 等...):let data : NSData = NSData(contentsOfURL: NSURL(string: "https://s3-ak.buzzfed.com/static/2014-07/16/9/enhanced/webdr08/edit-14118-1405517808-7.jpg")!)!
准备您的图像将被保存的路径,并从
中获取您的data
object:UIImage
let filename = "picture-test.jpg" let paths = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true) let filepath = paths.first! + "/" + filename let img : UIImage = UIImage(data: data)!
假设您已经设置了
topShelfStyle
属性,使用方法TVTopShelfImageSizeForShape
获取顶部货架图像的大小。这将是您的图像上下文的大小:let shapeSize : CGSize = TVTopShelfImageSizeForShape(contentItem.imageShape, self.topShelfStyle)
创建
shapeSize
大小的图像上下文并将下载的图像绘制到上下文矩形中。您可以在此处对图像进行所有修改,以将其调整为所需的大小。在此示例中,我从 Instagram 获取了一张方形图像,并在右侧和左侧放置了白色信箱带。UIGraphicsBeginImageContext(shapeSize) let imageShapeInRect : CGRect = CGRectMake((shapeSize.width-shapeSize.height)/2,0,shapeSize.height,shapeSize.height) img.drawInRect(imageShapeInRect) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext()
最后把这张图片存入你的
NSCacheDirectory
,图片路径设置为contentItem
的imageURL
。UIImageJPEGRepresentation(newImage, 0.8)!.writeToFile(filepath, atomically: true) contentItem.imageURL = NSURL(fileURLWithPath: filepath)
我想补充 Nicola Giancecchi 的回答。 Top Shelf 扩展将在非主线程上 运行,因此使用 UIImageJPEGRepresentation
或 UIImagePNGRepresentation
等方法有时会停止 Top Shelf 线程在控制台中打印此内容:
Program ended with exit code: 0
要解决这个问题,您可以像这样包装您的代码:
DispatchQueue.main.sync {
UIImageJPEGRepresentation(newImage, 0.8)!.writeToFile(filepath, atomically: true)
let imageURL = NSURL(fileURLWithPath: filepath)
if #available(tvOSApplicationExtension 11.0, *) {
contentItem.setImageURL(imageURL, forTraits: .userInterfaceStyleLight)
contentItem.setImageURL(imageURL, forTraits: .userInterfaceStyleDark)
} else {
contentItem.imageURL = imageURL
}
}