5

我正在适应 Swift 一种我在互联网上找到的 ObjectiveC 方法(也许在这里,我不记得了),当我为 iPad Air 构建它时,它运行完美,但是当我尝试在 iPad 2 或 iPad 上运行它时Retina 它给出了 4 个错误(2 个不同的错误,每个错误两次):

/* Scale and crop image */
func imageByScalingAndCroppingForSize(#originalImage: UIImage, size:CGSize) -> UIImage {
    let sourceImage = originalImage
    var newImage: UIImage
    let imageSize: CGSize = sourceImage.size
    let width: Double = Double(imageSize.width)
    let height: Double = Double(imageSize.height)
    var targetWidth: Double = Double(size.width)
    var targetHeight: Double = Double(size.height)
    var scaleFactor: Double = 0.0
    var scaledWidth: Double = targetWidth
    var scaledHeight: Double = targetHeight
    var thumbnailPoint: CGPoint = CGPointMake(0.0, 0.0)

    if (imageSize != size) {
        let widthFactor: Double = Double(targetWidth / width)
        let heightFactor: Double = Double(targetHeight / height)

        if (widthFactor > heightFactor) { // Scale to fit height
            scaleFactor = widthFactor
        }else{ // Scale to fit width
            scaleFactor = heightFactor
        }

        scaledWidth = Double(width * scaleFactor)
        scaledHeight = Double(height * scaleFactor)

        // Center the image
        if (widthFactor > heightFactor) {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5 // Could not find an overload for '*' that accepts the supplied arguments
        }else{
            if (widthFactor < heightFactor) {
                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5 // Could not find an overload for '*' that accepts the supplied arguments
            }
        }
    }

    UIGraphicsBeginImageContext(size)

    var thumbnailRect: CGRect = CGRectZero
    thumbnailRect.origin = thumbnailPoint
    thumbnailRect.size.width = scaledWidth // Cannot convert the expression's type '()' to type 'CGFloat'
    thumbnailRect.size.height = scaledHeight // Cannot convert the expression's type '()' to type 'CGFloat'

    sourceImage.drawInRect(thumbnailRect)

    newImage = UIGraphicsGetImageFromCurrentImageContext()

    if (newImage == nil) {
        println("could not scale image")
    }

    // pop the context to get back to the default
    UIGraphicsEndImageContext()

    return newImage
}
4

2 回答 2

5

对于这两个错误,请尝试创建新CGFloat的 s。

CGFloat(0.5)
CFFloat(scaledWidth)
于 2014-06-15T22:19:56.043 回答
0

发生这种情况的原因是因为 Double 没有隐式转换为 32 位上的 CGFloat。但是它是在 64 位上的。

您可以使用显式类型, var x:CGFloat而不是隐式推断,因为无论架构如何,所有带小数的数字都变为 Double。

于 2014-07-07T12:11:05.250 回答