【问题标题】:UIImage created doesn't appear as I would have expected?创建的 UIImage 没有像我预期的那样出现?
【发布时间】: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


【解决方案1】:

我认为您正在混合图像大小、所需图像大小、屏幕/视图大小...

在 Playground 中试试这个...我添加了名为“orig2048x1024.png”的 2048x768“1 2”图像

import UIKit
import PlaygroundSupport

var screenSize = CGSize(width: 1920, height: 1080)

let containerView = UIView(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height))
containerView.backgroundColor = UIColor.green


func chopImageFromRect(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: newSize.height))
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage!
}


var finalImage: UIImage?

// original image size is 2048 x 768
let image2S = UIImage(named: "orig2048x768.png")

// target image size should be 3840 x 1080
let targetImageSize = CGSize(width: screenSize.width * 2.0, height: screenSize.height)

if let sz = image2S?.size {

    // get the left side - x: 0 y: 0 / 1024 x 768 - of image
    let leftHalf = chopImageFromRect(image: image2S!, newRect: CGRect(x: 0, y: 0, width: sz.width / 2, height: sz.height))

    // get the right side - x: 1024 y: 0 / 1024 x 768 - of image
    let rightHalf = chopImageFromRect(image: image2S!, newRect: CGRect(x: sz.width / 2, y: 0, width: sz.width / 2, height: sz.height))

    // target size for each is Half of the Target Image Size (double the screen width x the screen height)
    let targetHalfSize = CGSize(width: targetImageSize.width / 2, height: targetImageSize.height)

    // scale each half to targetHalfSize
    let newLH = resizeLONGImage(image: leftHalf, newSize: targetHalfSize)
    let newRH = resizeLONGImage(image: rightHalf, newSize: targetHalfSize)

    // combine the two newly-scaled halfs
    finalImage = combine2LONGImage(imageUn: newLH, imageDeux: newRH)

    // create an UIImageView the same size as the screen
    let imgView = UIImageView(frame: containerView.bounds)

    // set contentMode to .left
    imgView.contentMode = .left

    // set the image of the image view
    imgView.image = finalImage

    // add the image view to the screen
    containerView.addSubview(imgView)

}


PlaygroundPage.current.liveView = containerView
PlaygroundPage.current.needsIndefiniteExecution = true

【讨论】:

  • 是的,也许是我从未发现的错误!谢谢!!改了,可惜没修。也会更新问题代码!!
  • 等等,我刚刚在代码中看到另一个错误;我正在重用原始图像,将其更改为改进,但仍然不对。它的高度正确但宽度不正确!
  • 唐,我找到了; leftImage 太大。它在踪迹中。我也检查你的答案。如果它有效,我也将其标记为已接受的答案!谢谢你的时间!!非常感激。也将我的代码更正为工作副本!
猜你喜欢
  • 2016-06-19
  • 2013-04-11
  • 1970-01-01
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多