如何使用xcassets构建框架

时间:2017-11-27 10:01:18

标签: ios swift xcasset

我尝试使用自己的图像构建一个框架,它编译得很好。但是,当我将我的框架包含在另一个项目中时,它会在加载图像时崩溃,任何想法?

ImageTest(我的框架)

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Material Design</title>
    <link rel="stylesheet" href="materials.css">
</head>

<body>
    <div class="header">
        <input type="checkbox" id="menu" class="menu">
        <label class="menu-cirkel" for="menu">
            <span class="menu-streep"></span>
        </label>
        <nav class="drawer">
            <ul>
                <li>Card 1</li>
                <li>Card 2</li>
                <li>Card 3</li>
            </ul>
        </nav>
    </div>
    <div class="tabs">
        <div class="tab" id="tab1" onclick="document.getElementById('card1').display='block';
			 document.getElementById('card2').display='none';
			 document.getElementById('card3').display='none';">
            Tab 1
        </div>
        <div class="tab" id="tab2" onclick="document.getElementById('card1').display='none';
			 document.getElementById('card2').display='block';
			 document.getElementById('card3').display='none';">
            Tab 2
        </div>
        <div class="tab" id="tab3" onclick="document.getElementById('card1').display='none';
			 document.getElementById('card2').display='none';
		   	 document.getElementById('card3').display='block';">
            Tab 3
        </div>
    </div>
    <div class="cards" id="card1" style="display: none;">
        <div class="content-left">
            Card 1
            <div class="x-icon" onclick="document.getElementById('card1').style.display='none';">
                x
            </div>
        </div>
        <div class="content-right">Content 1 </div>
    </div>
    <div class="cards" id="card2" style="display: none;">
        <div class="content-left">
            Card 2
            <div class="x-icon" onclick="document.getElementById('card2').style.display='none';">
                x
            </div>
        </div>
        <div class="content-right">Content 2 </div>
    </div>
    <div class="cards" id="card3" style="display: none;">
        <div class="content-left">
            Card 3
            <div class="x-icon" onclick="document.getElementById('card3').style.display='none';">
                x
            </div>
        </div>
        <div class="content-right">Content 3</div>
    </div>
</body>
</html>

我的项目

public class ImageTest {
    open func getImage() {
        return #imageLiteral(resourceName: "IMG_0745")
    }
}

enter image description here

3 个答案:

答案 0 :(得分:4)

图像位于ImageTest框架中。当您执行此代码return #imageLiteral(resourceName: "IMG_0745")时,它会查看ImageTest项目的Image.xcassets,当它在那里找不到图像时,会导致崩溃。

更改您的代码以使用init function

的捆绑参数
open func getImage() {
        return UIImage(named:"IMG_0745", bundle:Bundle(for: self), compatibleWith:nil)
    }

答案 1 :(得分:1)

现在在Swift 4中是

UIImage(named: "IMG_0745", in: Bundle(for:self), compatibleWith: nil)

答案 2 :(得分:1)

Swift 5 中,

UIImage(named: "IMG_0745", in: Bundle(for: xxxYourViewController.self), compatibleWith: nil)