gpt4 book ai didi

objective-c - 将 swift 类导入 Objective-C 可以,但某些自定义文件不行

转载 作者:行者123 更新时间:2023-11-30 12:59:29 27 4
gpt4 key购买 nike

我的项目有 Objective-C 文件和 Swift 文件(混合搭配,感谢 https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html )。

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

我的 Controller 文件夹中的任何 swift 类都包含在 my-project.swift.h 中我可以在任何 Objective-C 类中使用它,没有任何问题。但是,这是问题,我不能在我自己创建的 Helper 文件夹中使用 swift 类(它位于 Controller 文件夹之外),所以我不能在我的 Objective-C 类(class)上使用它。这里有什么解决办法吗?这是我的帮助文件

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);
}
}

最佳答案

我明白了!只需更改此代码

public class imageHelper{

进入此

class imageHelper: NSObject{

关于objective-c - 将 swift 类导入 Objective-C 可以,但某些自定义文件不行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40014773/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com