2016-10-13 65 views
0

我的项目有Objective-C文件和Swift文件(混合搭配,感谢https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html)。import swift class into Objective-C works,but some custom file not

现在,我在我的项目中添加一个名为'Helper'的新文件夹,以包含我可以在任何类中使用的函数。

我的Controller文件夹中的任何快速类都包含在my-project.swift.h 中,我可以在任何Objective-C类中使用它,而不会有任何问题。但是,这里是问题所在,我不能在我自己创建的Helper文件夹(它位于Controller文件夹之外)中使用swift类,所以我无法在我的Objective-C类中使用它。任何解决方案?这里是我的帮手文件

import Foundation 
public class imageHelper{ 
    func ResizeImage(image: UIImage, targetSize: CGSize) -> UIImage { 
    let size = image.size 

    let widthRatio = targetSize.width/image.size.width 
    let heightRatio = targetSize.height/image.size.height 

    // Figure out what our orientation is, and use that to form the rectangle 
    var newSize: CGSize 
    if(widthRatio > heightRatio) { 
     newSize = CGSizeMake(size.width * heightRatio, size.height * heightRatio) 
    } else { 
     newSize = CGSizeMake(size.width * widthRatio, size.height * widthRatio) 
    } 

    // This is the rect that we've calculated out and this is what is actually used below 
    let rect = CGRectMake(0, 0, newSize.width, newSize.height) 

    // Actually do the resizing to the rect using the ImageContext stuff 
    UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0) 
    image.drawInRect(rect) 
    let newImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    return newImage 
} 

func imageWithSize(image: UIImage,size: CGSize)->UIImage{ 
    if UIScreen.mainScreen().respondsToSelector("scale"){ 
     UIGraphicsBeginImageContextWithOptions(size,false,UIScreen.mainScreen().scale); 
    } 
    else 
    { 
     UIGraphicsBeginImageContext(size); 
    } 

    image.drawInRect(CGRectMake(0, 0, size.width, size.height)); 
    let newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return newImage; 
} 

//Summon this function VVV 
func resizeImageWithAspect(image: UIImage,scaledToMaxWidth width:CGFloat,maxHeight height :CGFloat)->UIImage 
{ 
    let oldWidth = image.size.width; 
    let oldHeight = image.size.height; 

    let scaleFactor = (oldWidth > oldHeight) ? width/oldWidth : height/oldHeight; 

    let newHeight = oldHeight * scaleFactor; 
    let newWidth = oldWidth * scaleFactor; 
    let newSize = CGSizeMake(newWidth, newHeight); 

    return imageWithSize(image, size: newSize); 
} 
} 
+0

检查您的构建阶段 - >编译源代码,并确保'包括imageHelper.swift'。 –

回答

0

我想通了! 只是更改此代码

public class imageHelper{ 

这个

class imageHelper: NSObject{ 
相关问题