将图像切成小块swift3/使用init时不明确((CGImage:比例:方向:)

将图像切成小块swift3/使用init时不明确((CGImage:比例:方向:),swift3,uiimage,Swift3,Uiimage,我正在尝试以下操作。建议的解决方案是为Swift 2编写的。我已将其更新为Swift 3,并在以下行中出现错误“init((CGImage:scale:orientation:)的使用不明确”: images.append(UIImage(CGImage: tileCgImage, scale: image.scale, orientation: image.imageOrientation)) 你知道怎么修理吗?下面是代码: import UIKit class ViewControlle

我正在尝试以下操作。建议的解决方案是为Swift 2编写的。我已将其更新为Swift 3,并在以下行中出现错误“init((CGImage:scale:orientation:)的使用不明确”:

images.append(UIImage(CGImage: tileCgImage, scale: image.scale, orientation: image.imageOrientation))
你知道怎么修理吗?下面是代码:

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func slice(image: UIImage, into howMany: Int) -> [UIImage] {
    let width: CGFloat
    let height: CGFloat

    switch image.imageOrientation {
    case .left, .leftMirrored, .right, .rightMirrored:
        width = image.size.height
        height = image.size.width
    default:
        width = image.size.width
        height = image.size.height
    }

    let tileWidth = Int(width / CGFloat(howMany))
    let tileHeight = Int(height / CGFloat(howMany))

    let scale = Int(image.scale)
    var images = [UIImage]()
    let cgImage = image.cgImage!

    var adjustedHeight = tileHeight

    var y = 0
    for row in 0 ..< howMany {
        if row == (howMany - 1) {
            adjustedHeight = Int(height) - y
        }
        var adjustedWidth = tileWidth
        var x = 0
        for column in 0 ..< howMany {
            if column == (howMany - 1) {
                adjustedWidth = Int(width) - x
            }
            let origin = CGPoint(x: x * scale, y: y * scale)
            let size = CGSize(width: adjustedWidth * scale, height: adjustedHeight * scale)
            let tileCgImage = cgImage.cropping(to: CGRect(origin: origin, size: size))!
            images.append(UIImage(CGImage: tileCgImage, scale: image.scale, orientation: image.imageOrientation))
            x += tileWidth
        }
        y += tileHeight
    }
    return images
}
导入UIKit
类ViewController:UIViewController{
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从nib执行任何其他设置。
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
func切片(图像:UIImage,分为多少:Int)->[UIImage]{
让宽度:CGFloat
让高度:CGFloat
切换image.imageOrientation{
大小写。左、.leftMirrored、.right、.rightMirrored:
宽度=image.size.height
高度=image.size.width
违约:
宽度=image.size.width
高度=image.size.height
}
让tileWidth=Int(宽度/CGFloat(多少))
让tileHeight=Int(高度/CGFloat(多少))
设scale=Int(image.scale)
var images=[UIImage]()
让cgImage=image.cgImage!
var调整高度=瓷砖高度
变量y=0
对于0中的行..<有多少行{
如果行==(数量-1){
调整高度=整数(高度)-y
}
var调整宽度=波浪宽度
变量x=0
对于0中的列..<有多少个{
如果列==(数量-1){
调整宽度=整数(宽度)-x
}
设原点=CGPoint(x:x*比例,y:y*比例)
let size=CGSize(宽度:调整宽度*比例,高度:调整高度*比例)
让tileCgImage=cgImage.croping(to:CGRect(origin:origin,size:size))!
images.append(UIImage(CGImage:tileCgImage,scale:image.scale,orientation:image.imageOrientation))
x+=波浪宽度
}
y+=瓷砖高度
}
返回图像
}

}在Swift 3中

您可以这样使用:

let image:UIImag = UIImage( cgImage: cgImage )

我只是想确保Rob的评论得到强调,因为这似乎是正确的答案。另外,从Swift 4开始,方法签名保留Rob提到的内容

罗布:
在Swift 3中,该函数的第一个标签现在是cgImage:,而不是cgImage:。请参见init(cgImage:scale:orientation:)

例如:

let resultUIImg = UIImage(cgImage: someCGImg!, scale: origUIImg.scale, orientation: origUIImg.imageOrientation)

在Swift 3中,该函数的第一个标签现在是
cgImage:
,而不是
cgImage:
。请参阅。在您提到的问题中,我提供了Swift 3和Swift 2的实现。我将尝试一下,谢谢!