#跟着晓明学鸿蒙# 鸿蒙Next开发中ohpm三方库使用指南之commonlibhar集成示例
·

前言
随着鸿蒙生态蓬勃发展,各种实用的鸿蒙三方库已成为开发者高效开发的利器。它不仅能拓展应用功能边界,还能大幅提升开发效率,不管你是刚接触鸿蒙开发的小白,还是开发时长两年半的老司机,掌握这些三方库,绝对能让你在鸿蒙开发上更加方便!博主在日常的工作开发中也也是如此,今天给大家分享的是commonlibhar三方库。
commonLibHar(harmony-utils (API12 - 5.0.3.906)
版本要求:HarmonyOS 5.0.3 Beta2 SDK,原样包含OpenHarmony SDK Ohos_sdk_public 5.0.3.131 (API Version 15 Beta2)
🏆简介
commonLibHar是一个公共的方法,如函数的封装
🌞下载安装
ohpm install @zyl/commonlibhar
🙏使用说明
微信Sdk二次封装-鸿蒙接入二次封装,点击此处跳转
qqSdk-鸿蒙接入二次封装-har-扫码登录、授权登录,点击此处跳转
微博分享,微博接入sdk,点击此处跳转
方法 bufferToFile,传入ArrayBuffer流,返回一个沙箱路径
使用场景,分享的时候存入沙箱,,如小红书的分享需要地址
- 路径如下,getContext().cacheDir + '/Photo' +
${Date.now()}hongmeng${Math.random()}IMG.jpg
- buffer放在沙箱路径中
- @param pathFormat(可选) 自定义沙箱路径 列如(getContext().cacheDir + '/Photo' +
${Date.now()}hongmeng${Math.random()}IMG.jpg
) - @param buffer
- @returns 获取图片沙箱路径
const path = getContext().cacheDir + '/Photo' + `${Date.now()}hongmeng${Math.random()}IMG.jpg`
const pixmapUri = await bufferToFile(buffer, path) // Buffer 放在这个沙箱里面
内存中图片数据过多或过大优化
app.setImageRawDataCacheSize(1204*1024*100) // 设置内存缓存解码图片数据**大小**上限,超过上限,删除最久未加载的图片数据缓存
app.setImageCacheCount(100) // 设置内存缓存解码图片数据**数量**上限,超过上限,删除最久未加载的图片数据缓存
方法 pixelMapToBuffer,传入image.PixelMap,返回一个ArrayBuffer
const ArrayBufferData: ArrayBuffer = await pixelMapToBuffer(PixelMap) // 拿普通的pixmap资源得到 dataBuffer
方法 base64ToPixelMap,传入base64,返回一个PixelMap
const PixelMap: image.PixelMap = await base64ToPixelMap(imgData)
方法 downFileUrlToPhotoAlbums,传入string[],也就是沙箱路径数组,临时弹窗授权下载缓存路径资源,批量下载图片
目前只支持图片类型
- @param pixmapUris 沙箱路径数组
- @param fn(可传) 回调
- @param types(可传)
- {
- subtype?:photoAccessHelper.PhotoSubtype, // 下载普通还是动态还是连拍照片文件类型。
- fileNameExtension?:string, //文件扩展名,例如'jpg'。
- title?:string // 图片或者视频的标题,不传入时由系统生成。参数规格为:- 不应包含扩展名。- 文件名字符串长度为1~255(资产文件名为标题+扩展名)。- 不允许出现非法字符,包括:. \ / : * ? " ' ` < > | { } [ ]
- }
- @returns 弹窗确认临时授权下载
// 简单使用
await downFileUrlToPhotoAlbums(pixmapUris, (resCode) => {
console.log('baocuncresCode',resCode)
if (resCode === '200') {
//图片保存成功
}else if (resCode === '204') {
//用户已取消
}else{
图片保存失败
}
})
// 完整开发分享
import { fileIo as fs, } from '@kit.CoreFileKit';
let buffer: ArrayBuffer = new ArrayBuffer(0)
try {
buffer =
await downloadImageWithUrl("https://i-avatar.csdnimg.cn/72ceb7652a604f8daa327a5c2d7169e5_weixin_42301175.jpg!1")
const path = getContext().cacheDir + '/Photo' + `IMG1.jpg`
const pixmapUri = await bufferToFile(buffer, path) // Buffer 放在这个沙箱里面
// 开发分享
await downFileUrlToPhotoAlbums([pixmapUri], (resCode) => {
AlertDialog.show({
message: 'baocuncresCode-resCode' + resCode
})
if (resCode === '200') {
fs.unlinkSync(path) // 释放缓存
//图片保存成功
} else if (resCode === '204') {
//用户已取消
} else {
}
})
} catch (e) {
}
方法 downloadImageWithUrl,传入网络图片,返回一个ArrayBuffer,
-
@param url
-
@returns ArrayBuffer
-
下载网络图片转化为 ArrayBuffer
module.json5配置网络权限{ "name": "ohos.permission.INTERNET" }
let buffer: ArrayBuffer = new ArrayBuffer(0)
try {
buffer = await downloadImageWithUrl(pixelBufferOrStr)
} catch (e) {
}
方法 downloadImageWithUrls,网络图片批量转ArrayBuffer
"name": "ohos.permission.INTERNET" // 网络权限
const buffers:ArrayBuffer[] = await downloadImageWithUrls([
"https://i-avatar.csdnimg.cn/72ceb7652a604f8daa327a5c2d7169e5_weixin_42301175.jpg!1",
"https://i-avatar.csdnimg.cn/72ceb7652a604f8daa327a5c2d7169e5_weixin_42301175.jpg!1",'https://i-avatar.csdnimg.cn/72ceb7652a604f8daa327a5c2d7169e5_weixin_42301175.jpg!1'])
console.log('得到arrayBuffer是网络图片批量转ArrayBuffer',buffers.length)
方法 downloadVideoWithFiles,传入网络图片,返回一个沙箱路径,
- @param urls:string 网络视频地址
- @param filePath:string(可不传) // 路径
- @returns Promise // 返回地址路径,可直接用于分享
const videoPath = await downloadVideoWithFiles('http://47.122.120.244/dist/video.MP4') // 可直接分享到微博
方法 downloadImageWithUrlsToFile,网络图片批量转沙箱地址
"name": "ohos.permission.INTERNET" // 网络权限
const strs:string[] = await downloadImageWithUrlsToFile([
"https://i-avatar.csdnimg.cn/72ceb7652a604f8daa327a5c2d7169e5_weixin_42301175.jpg!1",
'https://i-avatar.csdnimg.cn/72ceb7652a604f8daa327a5c2d7169e5_weixin_42301175.jpg!1'])
// 保存到相册
await downFileUrlToPhotoAlbums(strs, (resCode) => {
if (resCode === '200') {
//图片保存成功
} else if (resCode === '204') {
//用户已取消
} else {
}
})
方法 downloadImageWithUrlsToPhotoAlbums,网络图片批量存相册
downloadImageWithUrlsToPhotoAlbums([
"https://i-avatar.csdnimg.cn/72ceb7652a604f8daa327a5c2d7169e5_weixin_42301175.jpg!1",
'https://i-avatar.csdnimg.cn/72ceb7652a604f8daa327a5c2d7169e5_weixin_42301175.jpg!1'])
.then((resCode) => {
if (resCode === '200') {
//图片保存成功
} else if (resCode === '204') {
//用户已取消
} else {
}
})
loading提示组件
基础用法
DotsLoading({
visible: this.isShowLoading2,delayed:100
})
高阶用法-自定义
DotsLoading({
visible: this.isShowLoading,delayed:150,
imgBuilder: this.imgBuilder
})
@Builder imgBuilder() {
Column() {
Image($r('app.media.xiangjiao'))
.rotate({ angle: this.angle })
.animation({
duration: 1_500,
delay: 200,
iterations: -1,
curve: Curve.FastOutSlowIn
})
.onAppear(() => {
this.angle = 360
})
}
.padding(16)
.borderRadius(12)
.backgroundColor('#99283044')
.backgroundBlurStyle(BlurStyle.BACKGROUND_THIN)
.width(70)
.height(70)
}
🙏测试用例-案例组件
exampleIndex()
更多推荐
所有评论(0)