5

I was adapting to Swift an ObjectiveC method I found on the internet (maybe here, I can't remember it) and when I build it for iPad Air, it runs perfectly, but when I try to run it on an iPad 2 or iPad Retina it gives 4 errors (2 different errors, each one twice):

/* 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
}
rulilg
  • 1,604
  • 4
  • 15
  • 19
  • See this: http://stackoverflow.com/a/24196575/716216 – Mick MacCallum Jun 15 '14 at 22:06
  • possible duplicate of [Swift UIColor initializer - compiler Error only when targeting iPhone5s](http://stackoverflow.com/questions/24196528/swift-uicolor-initializer-compiler-error-only-when-targeting-iphone5s) – Andrew Jun 24 '14 at 03:16

2 Answers2

5

For both errors, try creating new CGFloats.

CGFloat(0.5)
CFFloat(scaledWidth)
Mundi
  • 79,884
  • 17
  • 117
  • 140
0

The reason this happens is because of Double isn't implicitly casted to a CGFloat on 32bit. It is however on 64bit.

You can use explicit types like var x:CGFloat instead of inferring implicitly since all numbers with decimals become Double regardless of architecture.

Seivan
  • 668
  • 6
  • 13