Tai*_*mal 5 apple-tv swift tvos
我正在Apple tvOS中实现TopShelf.我已经下载的图像和分配给IMAGEURL的TVContentItems.下载的图像宽高比不适合TopShelf图像.我试图通过在图像链接上附加width + height来更改大小.
www.mydownloadedimages.com/{width}x{height}
但它没有用.
我可以以任何其他方式在客户端调整大小.在TVContentItem类中,我只有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
Run Code Online (Sandbox Code Playgroud)
当此属性的值对Top Shelf样式无效时,系统保留以任何方式缩放图像的权限.
您说对了TVContentItem,没有UIImage类型属性。由于TVContentItem还接受该imageURL属性中的本地文件URL,因此可以采用以下解决方法:
UIImage从互联网上抢NSCacheDirectoryimageURL。步骤如下:
让我们创建一个TVContentItem对象:
let identifier = TVContentIdentifier(identifier: "myPicture", container: wrapperID)!
let contentItem = TVContentItem(contentIdentifier: identifier )!
Run Code Online (Sandbox Code Playgroud)设置contentItem的imageShape:
contentItem.imageShape = .HDTV
Run Code Online (Sandbox Code Playgroud)从Internet抓取图像。实际上,我是同步进行的,您也可以尝试使用其他异步方法来获取该信息(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")!)!
Run Code Online (Sandbox Code Playgroud)准备保存图像的路径,并UIImage从data对象中获取图像:
let filename = "picture-test.jpg"
let paths = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)
let filepath = paths.first! + "/" + filename
let img : UIImage = UIImage(data: data)!
Run Code Online (Sandbox Code Playgroud)假设您已经设置了topShelfStyle属性,请使用方法获取顶层货架图像的大小TVTopShelfImageSizeForShape。这将是图像上下文的大小:
let shapeSize : CGSize = TVTopShelfImageSizeForShape(contentItem.imageShape, self.topShelfStyle)
Run Code Online (Sandbox Code Playgroud)创建shapeSize大小合适的图像上下文,并将下载的图像绘制到上下文rect中。您可以在此处对图像进行所有修改,以将其调整为所需的尺寸。在此示例中,我从Instagram拍摄了方形图像,并在左右两侧放置了白色信箱带。
UIGraphicsBeginImageContext(shapeSize)
let imageShapeInRect : CGRect = CGRectMake((shapeSize.width-shapeSize.height)/2,0,shapeSize.height,shapeSize.height)
img.drawInRect(imageShapeInRect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Run Code Online (Sandbox Code Playgroud)最后,将图像保存到您NSCacheDirectory,并设定为图像路径contentItem的imageURL。
UIImageJPEGRepresentation(newImage, 0.8)!.writeToFile(filepath, atomically: true)
contentItem.imageURL = NSURL(fileURLWithPath: filepath)
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
766 次 |
| 最近记录: |