创建的 UIImage 没有像我预期的那样出现?

UIImage created doesn't appear as I would have expected?

iOS10.2swift3.0

我正在使用这些例程来剪切、调整大小和重新组合图像以在 AppleTV 上显示。但是尽管它报告图像大小正确,但它并不像我 expected/wanted.

那样工作

我在这里错过了什么?

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 宽度,相同高度],它就会起作用。但是,如果我上传的图像是子集,请尝试裁剪 + 合并它,不,它不起作用。

刚刚在左侧添加了一段代码,但也许...仍然无法正常工作。我创建的第一张图片尺寸准确,看起来像这样......

原来是这样的...

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

在游乐场试试这个...我添加了名为 "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