上次我們創建了一個簡單的Weex的demo。
一、常用的類
WXSDKEngine:SDK開放的絕大多數接口都在此有聲明。
WXLog: 用來打印日志。
WXDebugTool: weex提供的對外調試工具。
WXAppConfiguration: 使用weex開發的業務性配置。
二、添加圖片
1、瀏覽器查看
建議大家可以看下阿里團隊的weex文章。
在上篇的helloweex.we 中的div標簽中 加入圖片image標簽和thumbnail樣式,全部代碼:
<template>
<div>
<image class="thumbnail" src="https://img.alicdn.com/tps/TB15vyaLpXXXXXXXFXXXXXXXXXX-198-46.png"></image>
<text class="title" onclick="onClickTitle">Hello Weex</text>
</div>
</template>
<style>
.title { color: red; }
.thumbnail { width: 100; height: 30; }
</style>
<script>
module.exports = {
methods: {
onClickTitle: function (e) {
console.log(e);
alert('title clicked.');
}
}
}
</script>
運行weex helloWeex.we 后,效果如下:
2、iOS端顯示圖片
我們執行 weex helloWeex.we -o helloWeex.js 。然后把生成的helloWeex.js 替換到項目中。
然后在iPhone上運行 會發現 圖片並沒有顯示出來。
首先我們新建一個 WXImageDownloader 類,用來實現圖片下載協議WXImgLoaderProtocol。
代碼如下:
#import <WeexSDK/WeexSDK.h>
@interface WXImageDownloader : NSObject <WXImgLoaderProtocol>
@end
在.m文件中實現WXImgLoaderProtocol協議的downloadImageWithURL方法。用SDWebImage下載圖片。
#import "WXImageDownloader.h"
#import <SDWebImage/SDWebImageManager.h>
@implementation WXImageDownloader
- (id<WXImageOperationProtocol>)downloadImageWithURL:(NSString *)url
imageFrame:(CGRect)imageFrame
userInfo:(NSDictionary *)options
completed:(void(^)(UIImage *image, NSError *error, BOOL finished))completedBlock {
return (id<WXImageOperationProtocol>)
[[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:url]
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize)
{
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL)
{
if (completedBlock) {
completedBlock(image, error, finished);
}
}];
}
@end
然后在AppDelegate中注冊Handler:
[WXSDKEngine registerHandler:[WXImageDownloader new] withProtocol:@protocol(WXImgLoaderProtocol)];
運行后的效果為:
源代碼的地址還是 上篇文章中的github地址。