【发布时间】:2017-02-20 12:22:41
【问题描述】:
iOS 10.2 Swift 3.0
我正在使用这些例程来剪切、调整大小和重新组合要在 AppleTV 上显示的图像。但尽管它报告的图像尺寸正确,但它并没有按照我的预期/想要的方式工作。
我错过了什么?
func cropImage(image: UIImage, newRect: CGRect) -> UIImage {
let img = CIImage(image: image)!.cropping(to: newRect)
let image = UIImage(ciImage: img, scale: 1.0, orientation: image.imageOrientation)
return image
}
func combine2LONGImage(imageUn: UIImage, imageDeux: UIImage) -> UIImage {
let size = CGSize(width: imageUn.size.width + imageDeux.size.width, height: imageUn.size.height)
UIGraphicsBeginImageContext(size)
imageUn.draw(in: CGRect(x: 0, y: 0, width: imageUn.size.width, height: imageUn.size.height))
imageDeux.draw(in: CGRect(x: imageUn.size.width, y: 0, width: imageDeux.size.width, height: imageDeux.size.height))
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
func resizeLONGImage(image: UIImage, newSize: CGSize) -> UIImage {
UIGraphicsBeginImageContext(newSize)
image.draw(in: CGRect(x:0, y:0, width: newSize.width, height: self.view.bounds.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
let leftSide = CGRect(x: 0, y: 0, width: (image2S?.size.width)!/2, height: self.view.bounds.height)
let leftImage = self.cropImage(image: image2S!, newRect: leftSide)
let rightSide = CGRect(x: (image2S?.size.width)!/2, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
let rightImage = self.cropImage(image: image2S!, newRect: rightSide)
print("20022017 \(leftImage) \(rightImage)")
let newLeftImage = self.resizeLONGImage(image: leftImage, newSize: CGSize(width: self.view.bounds.width, height: self.view.bounds.height))
let newRightImage = self.resizeLONGImage(image: rightImage, newSize: CGSize(width: self.view.bounds.width, height: self.view.bounds.height))
let superNewImage = self.combine2LONGImage(imageUn: newLeftImage, imageDeux: newRightImage)
print("19022017 newimage \(self.image2P.bounds) \(leftImage.size.width) \(newLeftImage.size.height) \(superNewImage.size)")
self.image2P.image = superNewImage
self.image2P.contentMode = .left
self.image2P.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
返回的图像 [是正确的尺寸 [是 AppleTV 屏幕的两倍,相同的高度] ...但是 contentMode 让我失望了,它调整了图像的大小,使其看起来适合屏幕! ]。我做了一个快照,我得到了这个......
是的,它是 3 和 4,但它与 1 和 2 相同。
控制台正在报告?
19022017 previewPane 1920.0 1080.0 1920.0 1080.0
20022017 Optional(<UIImage: 0x60000009d9c0>, {1920, 768}) Optional(<UIImage: 0x60000009da10>, {1024, 768})
19022017 newimage (0.0, 0.0, 1920.0, 1080.0) 1920.0 1080.0 (3840.0, 1080.0)
superNewImage 是 3840 宽和 1080 高,我说只是左对齐,但它在做什么?
如果我加载我用 Paint 创建的图像,说它的大小相同,并在屏幕上显示它,它会按我的预期工作,一半在屏幕上,一半在屏幕上。
我开始的原始图像在这里。
我要做的是只在屏幕上显示第一个数字,以便应用程序的用户可以横向滚动到第二个数字。我开始工作的滚动,一些修复工作。如果我上传具有正确尺寸 [2 x 宽度,相同高度] 的预设图像,它就会起作用。但是,如果我上传的图像是一个子集,请尝试裁剪 + 组合它,不,它不起作用。
刚刚添加了一段左侧代码,另一个糟糕的可能......仍然无法正常工作。我创建的第一张图片的确切尺寸有效,它看起来像这样......
原来的样子是这样的……
【问题讨论】:
-
好的 - 有点不清楚你从什么开始,你想以什么结束......你能链接到你的原始图像,以及你期望它如何完成吗?而且,我假设您的
cropImage()函数与self.chopImageFromRect()相同? -
hmmm...您的原始图像是
2048 x 768是吗?你想不按比例缩放到3840 x 1080吗?或者,您是否希望它按比例缩放到2,880 x 1080,然后每一半在最终3840 x 1080图像的每一侧水平居中?或者,您是否不想要缩放图像,而是在最终3840 x 1080图像的每一侧将每一半垂直和水平居中? -
我想要缩放图像,使其适合 AppleTV 作为两个独立的 AppleTV 屏幕。所以单个图像是 3840 x 1080。我希望该图像最初出现在屏幕上,就好像它只有 1920 x 1080,让用户可以选择通过滚动查看另一半 1920 x 1080。我拿了 2048 x 768,把它切成两半 (1024 x 768)同时,尽管只使用了 = left 的 contentMode。
-
是的,chop 和crop 是一样的。
标签: ios swift uiimage uigraphicscontext