0

我正在 swift Project 中使用 SVGKit。我按照以下( https://github.com/SVGKit/SVGKit)正确地将 SVGKit 框架和 3rd 方资源文件夹导入到我的项目中。

我在项目中创建了一个包含 .svg 图像集的文件夹,并且我正在使用以下代码将图像加载到 testIcon(ImageView) 中。

let lockImage: SVGKImage = SVGKImage(named: "LockIcon.svg")
let lockImageLocal: UIImage = lockImage.uiImage
testIcon.image = lockImageLocal

但我收到以下错误,

** +[SVGKImage imageWithSource:]、/Users/fss/Downloads/SVGKit-2.x/Source/SVGKImage.m:229 中的断言失败

*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效参数不满足:newSource!= nil”

请让我知道如何将.svg图像路径(在我的项目中)提到 SVGKImage 图像类或如何解决此问题。

4

1 回答 1

0

.svg从图像名称中删除。此方法named:不需要扩展。

或者你也可以试试这个方法...

斯威夫特 4

if let imagePath = Bundle.main.path(forResource: "imageName", ofType: "svg")     
{
  let lockImage: SVGKImage = SVGKImage(contentsOfFile: imagePath)
  let lockImageLocal: UIImage = lockImage.uiImage
  testIcon.image = lockImageLocal
}

obj-c 类型

NSString *imagePath = [NSBundle mainBundle][ pathForResource:@"imageName" ofType:@"svg"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
于 2018-02-20T08:28:51.153 回答