首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Weex基本操作

Weex基本操作

作者头像
码客说
发布2019-10-22 14:45:51
发布2019-10-22 14:45:51
8410
举报
文章被收录于专栏:码客码客

常用的几个网址

安装运行

weex-toolkit 是官方提供的一个脚手架命令行工具,你可以使用它进行 Weex 项目的创建,调试以及打包等功能。

代码语言:javascript
复制
npm install weex-toolkit -g --registry=https://registry.npm.taobao.org

创建项目

代码语言:javascript
复制
weex create demo001

进入项目

代码语言:javascript
复制
cd demo001

运行项目

代码语言:javascript
复制
npm run web

iOS端 使用

下载源代码

代码语言:javascript
复制
git clone https://github.com/apache/incubator-weex.git

incubator-weex–>ios–>sdk–>WeexSDK 目录放到项目里

incubator-weex–>pre-build–>native-bundle-main.js文件放到项目里

这里之所以不用pod引用 是因为WXStorageModule这个类是私有的 所以只能用源代码了

具体代码

添加引用

代码语言:javascript
复制
#import "WeexSDK.h"
#import "WXStorageModule.h"

AppDelegate中初始化

代码语言:javascript
复制
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
代码语言:javascript
复制
func initWeex(){
    WXAppConfiguration.setAppGroup("com.saitongedu")
    WXAppConfiguration.setAppName("SaitongPlatform")
    WXAppConfiguration.setAppVersion("1.0")
    WXSDKEngine.registerHandler(MyImgLoader.init(), with: WXImgLoaderProtocol.self)
    WXSDKEngine.registerDefaults()
    WXSDKEngine.initSDKEnvironment()
    WXLog.setLogLevel(.error)
}

其中 MyImgLoader 是用来处理图片的 不自定义的话 无法加载图片

代码如下

支持的图片类型

代码语言:javascript
复制
<image style="width:100px;height:100px" src="https://vuejs.org/images/logo.png"></image>
<image style="width:100px;height:100px" src="data:image/jpeg;base64,iVBORw0..."></image>
<image style="width: 100px; height: 100px;" src="xcassets:d_baibai"></image>

处理代码

代码语言:javascript
复制
import Foundation
import Alamofire

class MyImgLoader:NSObject,WXImgLoaderProtocol,WXImageOperationProtocol{
    var downloadRequest:DownloadRequest?
    
    func downloadImage(withURL url: String!, imageFrame: CGRect, userInfo options: [AnyHashable : Any]! = [:], completed completedBlock: ((UIImage?, Error?, Bool) -> Void)!) -> WXImageOperationProtocol! {
        if let url = url{
            if(url.hasPrefix("http")){
                let destination: DownloadRequest.DownloadFileDestination = { _, _ in
                    let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
                    let fileURL = documentsURL.appendingPathComponent("temp.png")
                    return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
                }
                
                self.downloadRequest = Alamofire.download(url, to: destination).response { response in
                    if response.error == nil, let imagePath = response.destinationURL?.path {
                        if let image = UIImage(contentsOfFile: imagePath){
                            completedBlock(image,nil,true)
                        }
                    }
                }
            }else if(url.hasPrefix("data")){
                var arr = url.split{ $0 == "," }.map { String($0) };
                let base64Str = arr[arr.count-1]
                if let data = Data.init(base64Encoded: base64Str){
                    if let image = UIImage.init(data: data){
                        completedBlock(image,nil,true)
                    }
                }
            }else if(url.hasPrefix("xcassets:")){
                let imageName = ZJStringUtils.replace(url, replaceStr: "xcassets:", withStr: "")
                if let image = UIImage.init(named: imageName){
                    completedBlock(image,nil,true)
                }
            }
            
        }
        
        return self
    }
    
    func cancel() {
        self.downloadRequest?.cancel()
    }
}

渲染页面

代码语言:javascript
复制
@IBOutlet weak var weexView: UIView!
var weexUrl:URL!

渲染的方法

代码语言:javascript
复制
func renderWeex(){
   let storage = WXStorageModule.init();
    storage.setItem("name", value: "张剑") { (result) in

    }
    weexInstance = WXSDKInstance.init()
    weexInstance.viewController = self
    weexInstance.frame = CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height-64)
    weexInstance.onCreate = {
        view in
        self.weexView.removeFromSuperview()
        self.weexView = view
        self.view.addSubview(self.weexView)
    }

    weexInstance.onFailed = {
        error in
        print("渲染失败")
    }

    weexInstance.renderFinish = {
        view in
        print("渲染完成")
    }

    self.weexUrl = Bundle.main.url(forResource: "index", withExtension: "js");
    weexInstance.render(with: self.weexUrl)
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-02-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 常用的几个网址
  • 安装运行
  • iOS端 使用
    • 下载源代码
    • 具体代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档