Weex系列二、显示图片
上次我们创建了一个简单的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地址。
作者:大河
本博客都为原创文章,转载请标明出处,谢谢。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?