2014-06-15 43 views
5

İnternette bulduğum Swift bir ObjectiveC yöntemine adapte oluyordum (belki burada hatırlayamıyorum) ve iPad için oluşturduğumda Hava, mükemmel çalışır, ancak ben bir iPad 2 ya da iPad Retina üzerinde çalıştırmayı denediğinizde bunu (iki kez her biri 2 farklı hatalar) 4 hataları verir: her iki hataları içinSwift - Aşırı yük bulamadı ... & İfade türünü çeviremiyor

/* 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 
} 
+0

Şunlara bakın: http://stackoverflow.com/a/24196575/716216 –

+0

olası yinelenen [Swift UIColor başlatıcısı - derleyici yalnızca iPhone5'leri hedeflerken hata] (http://stackoverflow.com/questions/24196528/swift- uicolor-başlatıcı-derleyici-hata-sadece-hedefleme-iphone5s) –

cevap

5

, yeni CGFloat s oluşturmayı deneyin . Double örtülü 32bit üzerinde CGFloat için Döküm değildir çünkü bu durumda

CGFloat(0.5) 
CFFloat(scaledWidth) 
+0

Milyon teşekkürler! – rulilg

0

nedenidir. Ancak 64bit üzerinde.

var x:CGFloat gibi müstehcen türlerini, çarpı işareti olan tüm sayılar, mimariye bakılmaksızın ikiye dönüştüğünden dolaylı olarak çıkarım yapmak yerine kullanabilirsiniz.

İlgili konular