1.补充option参数:placeholderObjectFit,errorholderObjectFit分别支持占位图填充效果和错误图填充效果
2.合并读写缓存策略CacheStrategy Signed-off-by: madixin <madixin@huawei.com>
This commit is contained in:
parent
8b4ba78d56
commit
dd24720c00
|
@ -1,6 +1,7 @@
|
||||||
|
|
||||||
## 3.0.0-rc.3
|
## 3.0.0-rc.3
|
||||||
- 将请求默认并行从64调整到8,减少对taskpool execute内存消耗
|
- 将请求默认并行从64调整到8,减少对taskpool execute内存消耗
|
||||||
|
- 补充option参数:placeholderObjectFit,errorholderObjectFit分别支持占位图填充效果和错误图填充效果
|
||||||
|
|
||||||
## 3.0.0-rc.2
|
## 3.0.0-rc.2
|
||||||
- 新增支持使用一个或多个图片变换,如模糊,高亮等
|
- 新增支持使用一个或多个图片变换,如模糊,高亮等
|
||||||
|
|
32
README.md
32
README.md
|
@ -138,22 +138,24 @@ ImageKnifeComponent({ ImageKnifeOption:
|
||||||
## 接口说明
|
## 接口说明
|
||||||
### ImageKnifeOption参数列表
|
### ImageKnifeOption参数列表
|
||||||
|
|
||||||
| 参数名称 | 入参内容 | 功能简介 |
|
| 参数名称 | 入参内容 | 功能简介 |
|
||||||
|-----------------------|--------------------------------|-----------------|
|
|-----------------------|---------------------------|-----------------|
|
||||||
| loadSrc | string、PixelMap、Resource | 主图展示 |
|
| loadSrc | string、PixelMap、Resource | 主图展示 |
|
||||||
| placeholderSrc | PixelMap、Resource | 占位图图展示(可选) |
|
| placeholderSrc | PixelMap、Resource | 占位图图展示(可选) |
|
||||||
| errorholderSrc | PixelMap、Resource | 错误图展示(可选) |
|
| errorholderSrc | PixelMap、Resource | 错误图展示(可选) |
|
||||||
| objectFit | ImageFit | 图片展示样式(可选) |
|
| objectFit | ImageFit | 主图填充效果(可选) |
|
||||||
| writeCacheStrategy | WriteCacheStrategyType | 写入缓存策略(可选) |
|
| placeholderObjectFit | ImageFit | 占位图填充效果(可选) |
|
||||||
| onlyRetrieveFromCache | boolean | 是否跳过网络和本地请求(可选) |
|
| errorholderObjectFit | ImageFit | 错误图填充效果(可选) |
|
||||||
|
| writeCacheStrategy | CacheStrategyType | 写入缓存策略(可选) |
|
||||||
|
| onlyRetrieveFromCache | boolean | 是否跳过网络和本地请求(可选) |
|
||||||
| customGetImage | (context: Context, src: string | 自定义下载图片(可选) | | Resource | 错误占位图数据源 |
|
| customGetImage | (context: Context, src: string | 自定义下载图片(可选) | | Resource | 错误占位图数据源 |
|
||||||
| border | BorderOptions | 边框圆角(可选) |
|
| border | BorderOptions | 边框圆角(可选) |
|
||||||
| priority | taskpool.Priority | 加载优先级(可选) |
|
| priority | taskpool.Priority | 加载优先级(可选) |
|
||||||
| context | common.UIAbilityContext | 上下文(可选) |
|
| context | common.UIAbilityContext | 上下文(可选) |
|
||||||
| progressListener | (progress: number)=>void | 进度(可选) |
|
| progressListener | (progress: number)=>void | 进度(可选) |
|
||||||
| signature | String | 自定义缓存关键字(可选) |
|
| signature | String | 自定义缓存关键字(可选) |
|
||||||
| headerOption | Array<HeaderOptions> | 设置请求头(可选) |
|
| headerOption | Array<HeaderOptions> | 设置请求头(可选) |
|
||||||
| transformation | PixelMapTransformation | 图片变换(可选) |
|
| transformation | PixelMapTransformation | 图片变换(可选) |
|
||||||
|
|
||||||
### ImageKnife接口
|
### ImageKnife接口
|
||||||
| 参数名称 | 入参内容 | 功能简介 |
|
| 参数名称 | 入参内容 | 功能简介 |
|
||||||
|
|
|
@ -92,6 +92,15 @@ struct Index {
|
||||||
uri: 'pages/ImageTransformation',
|
uri: 'pages/ImageTransformation',
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
Button("不同的ObjectFit").margin({top:10}).onClick(()=>{
|
||||||
|
router.push({
|
||||||
|
uri: 'pages/ObjectFitPage',
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,57 @@
|
||||||
|
import { ImageKnife, ImageKnifeComponent, ImageKnifeOption } from '@ohos/imageknife'
|
||||||
|
|
||||||
|
@Entry
|
||||||
|
@Component
|
||||||
|
struct ObjectFitPage {
|
||||||
|
@State imageKnifeOption: ImageKnifeOption = {
|
||||||
|
loadSrc: $r("app.media.app_icon"),
|
||||||
|
placeholderSrc: $r("app.media.loading"),
|
||||||
|
errorholderSrc: $r("app.media.app_icon"),
|
||||||
|
objectFit: ImageFit.Fill
|
||||||
|
}
|
||||||
|
|
||||||
|
build() {
|
||||||
|
Column() {
|
||||||
|
|
||||||
|
Button("主图Fill拉伸填充").onClick(()=>{
|
||||||
|
this.imageKnifeOption = {
|
||||||
|
loadSrc: $r("app.media.app_icon"),
|
||||||
|
placeholderSrc: $r("app.media.loading"),
|
||||||
|
errorholderSrc: $r("app.media.app_icon"),
|
||||||
|
objectFit: ImageFit.Fill
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
Button("占位图Contain保持比例填充").margin({top:10}).onClick(async () => {
|
||||||
|
ImageKnife.getInstance().removeAllMemoryCache()
|
||||||
|
await ImageKnife.getInstance().removeAllFileCache()
|
||||||
|
|
||||||
|
this.imageKnifeOption = {
|
||||||
|
loadSrc: "https://wx2.sinaimg.cn/mw690/006HyQKGgy1hnqp08dw09j30u04twu0x.jpg",
|
||||||
|
placeholderSrc: $r("app.media.app_icon"),
|
||||||
|
errorholderSrc: $r("app.media.app_icon"),
|
||||||
|
objectFit: ImageFit.Fill,
|
||||||
|
placeholderObjectFit: ImageFit.Contain
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
Button("错误图None不变化").margin({top:10}).onClick(() => {
|
||||||
|
this.imageKnifeOption = {
|
||||||
|
loadSrc: "http://xxxxx",
|
||||||
|
placeholderSrc: $r("app.media.loading"),
|
||||||
|
errorholderSrc: $r("app.media.app_icon"),
|
||||||
|
objectFit: ImageFit.Fill,
|
||||||
|
errorholderObjectFit: ImageFit.None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
ImageKnifeComponent({
|
||||||
|
imageKnifeOption: this.imageKnifeOption
|
||||||
|
}).width(300).height(200).border({width:1}).margin({top:50})
|
||||||
|
|
||||||
|
}
|
||||||
|
.height('100%')
|
||||||
|
.width('100%')
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,7 +12,7 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
import { ImageKnifeComponent, ImageKnife, ImageKnifeOption, ReadCacheStrategyType } from '@ohos/imageknife'
|
import { ImageKnifeComponent, ImageKnife, ImageKnifeOption, CacheStrategy } from '@ohos/imageknife'
|
||||||
|
|
||||||
@Entry
|
@Entry
|
||||||
@Component
|
@Component
|
||||||
|
@ -34,7 +34,7 @@ struct TestIsUrlExist {
|
||||||
Button("内存缓存获取gif").onClick(() => {
|
Button("内存缓存获取gif").onClick(() => {
|
||||||
ImageKnife.getInstance()
|
ImageKnife.getInstance()
|
||||||
.getCacheImage("https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658",
|
.getCacheImage("https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658",
|
||||||
ReadCacheStrategyType.Memory)
|
CacheStrategy.Memory)
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
this.source = data !== undefined ? data.source : $r("app.media.startIcon")
|
this.source = data !== undefined ? data.source : $r("app.media.startIcon")
|
||||||
})
|
})
|
||||||
|
@ -42,7 +42,7 @@ struct TestIsUrlExist {
|
||||||
Button("文件缓存获取gif").onClick(() => {
|
Button("文件缓存获取gif").onClick(() => {
|
||||||
ImageKnife.getInstance()
|
ImageKnife.getInstance()
|
||||||
.getCacheImage("https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658",
|
.getCacheImage("https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658",
|
||||||
ReadCacheStrategyType.File)
|
CacheStrategy.File)
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
this.source1 = data !== undefined ? data.source : $r("app.media.startIcon")
|
this.source1 = data !== undefined ? data.source : $r("app.media.startIcon")
|
||||||
})
|
})
|
||||||
|
@ -57,7 +57,7 @@ struct TestIsUrlExist {
|
||||||
Button("内存缓存获取").onClick(() => {
|
Button("内存缓存获取").onClick(() => {
|
||||||
ImageKnife.getInstance()
|
ImageKnife.getInstance()
|
||||||
.getCacheImage('https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp',
|
.getCacheImage('https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp',
|
||||||
ReadCacheStrategyType.Memory)
|
CacheStrategy.Memory)
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
this.source = data!.source
|
this.source = data!.source
|
||||||
})
|
})
|
||||||
|
@ -65,7 +65,7 @@ struct TestIsUrlExist {
|
||||||
Button("文件缓存获取").onClick(() => {
|
Button("文件缓存获取").onClick(() => {
|
||||||
ImageKnife.getInstance()
|
ImageKnife.getInstance()
|
||||||
.getCacheImage('https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp',
|
.getCacheImage('https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp',
|
||||||
ReadCacheStrategyType.File)
|
CacheStrategy.File)
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
this.source1 = data!.source
|
this.source1 = data!.source
|
||||||
})
|
})
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
"pages/TestPrefetchToFileCache",
|
"pages/TestPrefetchToFileCache",
|
||||||
"pages/TestIsUrlExist",
|
"pages/TestIsUrlExist",
|
||||||
"pages/TestHeader",
|
"pages/TestHeader",
|
||||||
"pages/ImageTransformation"
|
"pages/ImageTransformation",
|
||||||
|
"pages/ObjectFitPage"
|
||||||
]
|
]
|
||||||
}
|
}
|
|
@ -12,7 +12,7 @@ export { LogUtil } from './src/main/ets/utils/LogUtil'
|
||||||
|
|
||||||
export { IEngineKey } from './src/main/ets/key/IEngineKey'
|
export { IEngineKey } from './src/main/ets/key/IEngineKey'
|
||||||
|
|
||||||
export { ImageKnifeData , ReadCacheStrategyType} from "./src/main/ets/model/ImageKnifeData"
|
export { ImageKnifeData , CacheStrategy} from "./src/main/ets/model/ImageKnifeData"
|
||||||
|
|
||||||
export { PixelMapTransformation } from './src/main/ets/transform/PixelMapTransformation'
|
export { PixelMapTransformation } from './src/main/ets/transform/PixelMapTransformation'
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
import { ImageKnifeRequest } from './ImageKnifeRequest';
|
import { ImageKnifeRequest } from './ImageKnifeRequest';
|
||||||
import { ReadCacheStrategyType, ImageKnifeData, ImageKnifeRequestSource } from './model/ImageKnifeData';
|
import { CacheStrategy, ImageKnifeData, ImageKnifeRequestSource } from './model/ImageKnifeData';
|
||||||
import { MemoryLruCache } from './utils/MemoryLruCache';
|
import { MemoryLruCache } from './utils/MemoryLruCache';
|
||||||
import { IMemoryCache } from './utils/IMemoryCache'
|
import { IMemoryCache } from './utils/IMemoryCache'
|
||||||
import { FileCache } from './utils/FileCache';
|
import { FileCache } from './utils/FileCache';
|
||||||
|
@ -166,16 +166,16 @@ export class ImageKnife {
|
||||||
* @returns 图片数据
|
* @returns 图片数据
|
||||||
*/
|
*/
|
||||||
getCacheImage(loadSrc: string,
|
getCacheImage(loadSrc: string,
|
||||||
cacheType: ReadCacheStrategyType = ReadCacheStrategyType.Default): Promise<ImageKnifeData | undefined> {
|
cacheType: CacheStrategy = CacheStrategy.Default): Promise<ImageKnifeData | undefined> {
|
||||||
let option: ImageKnifeOption = {
|
let option: ImageKnifeOption = {
|
||||||
loadSrc: loadSrc
|
loadSrc: loadSrc
|
||||||
}
|
}
|
||||||
let engineKeyImpl: IEngineKey = new DefaultEngineKey()
|
let engineKeyImpl: IEngineKey = new DefaultEngineKey()
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (cacheType == ReadCacheStrategyType.Memory) {
|
if (cacheType == CacheStrategy.Memory) {
|
||||||
resolve(this.readMemoryCache(loadSrc, option, engineKeyImpl))
|
resolve(this.readMemoryCache(loadSrc, option, engineKeyImpl))
|
||||||
} else if (cacheType == ReadCacheStrategyType.File) {
|
} else if (cacheType == CacheStrategy.File) {
|
||||||
this.readFileCache(loadSrc, engineKeyImpl, resolve)
|
this.readFileCache(loadSrc, engineKeyImpl, resolve)
|
||||||
} else {
|
} else {
|
||||||
let data = this.readMemoryCache(loadSrc, option, engineKeyImpl)
|
let data = this.readMemoryCache(loadSrc, option, engineKeyImpl)
|
||||||
|
|
|
@ -22,7 +22,7 @@ import buffer from '@ohos.buffer';
|
||||||
import { FileCache } from './utils/FileCache';
|
import { FileCache } from './utils/FileCache';
|
||||||
import fs from '@ohos.file.fs';
|
import fs from '@ohos.file.fs';
|
||||||
import { ImageKnife } from './ImageKnife';
|
import { ImageKnife } from './ImageKnife';
|
||||||
import { ImageKnifeData, WriteCacheStrategyType } from './model/ImageKnifeData';
|
import { ImageKnifeData, CacheStrategy } from './model/ImageKnifeData';
|
||||||
import http from '@ohos.net.http';
|
import http from '@ohos.net.http';
|
||||||
import image from '@ohos.multimedia.image';
|
import image from '@ohos.multimedia.image';
|
||||||
import emitter from '@ohos.events.emitter';
|
import emitter from '@ohos.events.emitter';
|
||||||
|
@ -50,7 +50,7 @@ export class ImageKnifeDispatcher {
|
||||||
if (memoryCache !== undefined) {
|
if (memoryCache !== undefined) {
|
||||||
// 画主图
|
// 画主图
|
||||||
if (request.requestState === ImageKnifeRequestState.PROGRESS) {
|
if (request.requestState === ImageKnifeRequestState.PROGRESS) {
|
||||||
request.ImageKnifeRequestCallback?.showPixelMap(request.componentVersion, memoryCache.source)
|
request.ImageKnifeRequestCallback?.showPixelMap(request.componentVersion, memoryCache.source, requestSource)
|
||||||
|
|
||||||
if (requestSource == ImageKnifeRequestSource.SRC) {
|
if (requestSource == ImageKnifeRequestSource.SRC) {
|
||||||
request.requestState = ImageKnifeRequestState.COMPLETE
|
request.requestState = ImageKnifeRequestState.COMPLETE
|
||||||
|
@ -114,7 +114,7 @@ export class ImageKnifeDispatcher {
|
||||||
customGetImage: currentRequest.imageKnifeOption.customGetImage,
|
customGetImage: currentRequest.imageKnifeOption.customGetImage,
|
||||||
onlyRetrieveFromCache: currentRequest.imageKnifeOption.onlyRetrieveFromCache,
|
onlyRetrieveFromCache: currentRequest.imageKnifeOption.onlyRetrieveFromCache,
|
||||||
transformation:currentRequest.imageKnifeOption.transformation,
|
transformation:currentRequest.imageKnifeOption.transformation,
|
||||||
writeCacheStrategy: ImageKnife.getInstance().isFileCacheInit() ? currentRequest.imageKnifeOption.writeCacheStrategy : WriteCacheStrategyType.Memory, // 未初始化文件缓存时,不写文件缓存
|
writeCacheStrategy: ImageKnife.getInstance().isFileCacheInit() ? currentRequest.imageKnifeOption.writeCacheStrategy : CacheStrategy.Memory, // 未初始化文件缓存时,不写文件缓存
|
||||||
engineKey: this.engineKey,
|
engineKey: this.engineKey,
|
||||||
signature: currentRequest.imageKnifeOption.signature,
|
signature: currentRequest.imageKnifeOption.signature,
|
||||||
requestSource
|
requestSource
|
||||||
|
@ -149,7 +149,7 @@ export class ImageKnifeDispatcher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 保存文件缓存
|
// 保存文件缓存
|
||||||
if (requestJobResult.bufferSize > 0 && currentRequest.imageKnifeOption.writeCacheStrategy !== WriteCacheStrategyType.Memory) {
|
if (requestJobResult.bufferSize > 0 && currentRequest.imageKnifeOption.writeCacheStrategy !== CacheStrategy.Memory) {
|
||||||
ImageKnife.getInstance().saveWithoutWriteFile(requestJobResult.fileKey, requestJobResult.bufferSize)
|
ImageKnife.getInstance().saveWithoutWriteFile(requestJobResult.fileKey, requestJobResult.bufferSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -160,7 +160,7 @@ export class ImageKnifeDispatcher {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 保存内存缓存
|
// 保存内存缓存
|
||||||
if(currentRequest.imageKnifeOption.writeCacheStrategy !== WriteCacheStrategyType.File) {
|
if(currentRequest.imageKnifeOption.writeCacheStrategy !== CacheStrategy.File) {
|
||||||
ImageKnife.getInstance()
|
ImageKnife.getInstance()
|
||||||
.saveMemoryCache(this.engineKey.generateMemoryKey(imageSrc, requestSource, currentRequest.imageKnifeOption),
|
.saveMemoryCache(this.engineKey.generateMemoryKey(imageSrc, requestSource, currentRequest.imageKnifeOption),
|
||||||
ImageKnifeData)
|
ImageKnifeData)
|
||||||
|
@ -177,7 +177,7 @@ export class ImageKnifeDispatcher {
|
||||||
if (requestWithSource.source === ImageKnifeRequestSource.SRC || requestWithSource.source === ImageKnifeRequestSource.ERROR_HOLDER
|
if (requestWithSource.source === ImageKnifeRequestSource.SRC || requestWithSource.source === ImageKnifeRequestSource.ERROR_HOLDER
|
||||||
|| (requestWithSource.source === ImageKnifeRequestSource.PLACE_HOLDER && requestWithSource.request.requestState === ImageKnifeRequestState.PROGRESS)) {
|
|| (requestWithSource.source === ImageKnifeRequestSource.PLACE_HOLDER && requestWithSource.request.requestState === ImageKnifeRequestState.PROGRESS)) {
|
||||||
|
|
||||||
requestWithSource.request.ImageKnifeRequestCallback.showPixelMap(requestWithSource.request.componentVersion, ImageKnifeData.source)
|
requestWithSource.request.ImageKnifeRequestCallback.showPixelMap(requestWithSource.request.componentVersion, ImageKnifeData.source, requestWithSource.source)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (requestWithSource.source == ImageKnifeRequestSource.SRC) {
|
if (requestWithSource.source == ImageKnifeRequestSource.SRC) {
|
||||||
|
@ -244,7 +244,7 @@ async function requestJob(request: RequestJobRequest): Promise<RequestJobResult
|
||||||
LogUtil.log("customGetImage customGetImage");
|
LogUtil.log("customGetImage customGetImage");
|
||||||
resBuf = await request.customGetImage(request.context, request.src)
|
resBuf = await request.customGetImage(request.context, request.src)
|
||||||
// 保存文件缓存
|
// 保存文件缓存
|
||||||
if (resBuf !== undefined && request.writeCacheStrategy !== WriteCacheStrategyType.Memory) {
|
if (resBuf !== undefined && request.writeCacheStrategy !== CacheStrategy.Memory) {
|
||||||
let copyBuf = buffer.concat([buffer.from(resBuf)]).buffer; // IDE有bug,不能直接获取resBuf.byteLength
|
let copyBuf = buffer.concat([buffer.from(resBuf)]).buffer; // IDE有bug,不能直接获取resBuf.byteLength
|
||||||
bufferSize = copyBuf.byteLength
|
bufferSize = copyBuf.byteLength
|
||||||
FileCache.saveFileCacheOnlyFile(request.context, fileKey, resBuf)
|
FileCache.saveFileCacheOnlyFile(request.context, fileKey, resBuf)
|
||||||
|
@ -302,7 +302,7 @@ async function requestJob(request: RequestJobRequest): Promise<RequestJobResult
|
||||||
});
|
});
|
||||||
|
|
||||||
// 保存文件缓存
|
// 保存文件缓存
|
||||||
if (resBuf !== undefined && request.writeCacheStrategy !== WriteCacheStrategyType.Memory) {
|
if (resBuf !== undefined && request.writeCacheStrategy !== CacheStrategy.Memory) {
|
||||||
let copyBuf = buffer.concat([buffer.from(resBuf)]).buffer; // IDE有bug,不能直接获取resBuf.byteLength
|
let copyBuf = buffer.concat([buffer.from(resBuf)]).buffer; // IDE有bug,不能直接获取resBuf.byteLength
|
||||||
bufferSize = copyBuf.byteLength
|
bufferSize = copyBuf.byteLength
|
||||||
FileCache.saveFileCacheOnlyFile(request.context, fileKey, resBuf)
|
FileCache.saveFileCacheOnlyFile(request.context, fileKey, resBuf)
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
*/
|
*/
|
||||||
import taskpool from '@ohos.taskpool';
|
import taskpool from '@ohos.taskpool';
|
||||||
import common from '@ohos.app.ability.common'
|
import common from '@ohos.app.ability.common'
|
||||||
import { WriteCacheStrategyType } from './model/ImageKnifeData';
|
import { CacheStrategy } from './model/ImageKnifeData';
|
||||||
import { PixelMapTransformation } from './transform/PixelMapTransformation';
|
import { PixelMapTransformation } from './transform/PixelMapTransformation';
|
||||||
|
|
||||||
export interface HeaderOptions {
|
export interface HeaderOptions {
|
||||||
|
@ -35,13 +35,20 @@ export class ImageKnifeOption {
|
||||||
// 自定义缓存关键字
|
// 自定义缓存关键字
|
||||||
signature?: string;
|
signature?: string;
|
||||||
|
|
||||||
|
// 主图填充效果
|
||||||
objectFit?: ImageFit
|
objectFit?: ImageFit
|
||||||
|
|
||||||
|
// 占位图填充效果
|
||||||
|
placeholderObjectFit?: ImageFit
|
||||||
|
|
||||||
|
// 错误图填充效果
|
||||||
|
errorholderObjectFit?: ImageFit
|
||||||
|
|
||||||
customGetImage?: (context: Context, src: string | PixelMap | Resource) => Promise<ArrayBuffer | undefined>
|
customGetImage?: (context: Context, src: string | PixelMap | Resource) => Promise<ArrayBuffer | undefined>
|
||||||
|
|
||||||
border?: BorderOptions
|
border?: BorderOptions
|
||||||
// 缓存策略
|
// 缓存策略
|
||||||
writeCacheStrategy?: WriteCacheStrategyType
|
writeCacheStrategy?: CacheStrategy
|
||||||
// 仅使用缓存加载数据
|
// 仅使用缓存加载数据
|
||||||
onlyRetrieveFromCache?: boolean = false;
|
onlyRetrieveFromCache?: boolean = false;
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
*/
|
*/
|
||||||
import { ImageKnifeOption } from './ImageKnifeOption';
|
import { ImageKnifeOption } from './ImageKnifeOption';
|
||||||
import common from '@ohos.app.ability.common';
|
import common from '@ohos.app.ability.common';
|
||||||
|
import { ImageKnifeRequestSource } from './model/ImageKnifeData';
|
||||||
|
|
||||||
|
|
||||||
export class ImageKnifeRequest {
|
export class ImageKnifeRequest {
|
||||||
|
@ -63,5 +63,5 @@ export enum ImageKnifeRequestState {
|
||||||
|
|
||||||
|
|
||||||
export interface ImageKnifeRequestCallback {
|
export interface ImageKnifeRequestCallback {
|
||||||
showPixelMap: (version: number, pixelMap: PixelMap | string) => void;
|
showPixelMap: (version: number, pixelMap: PixelMap | string , requestSource: ImageKnifeRequestSource) => void;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ import { ImageKnifeRequest, ImageKnifeRequestState } from '../ImageKnifeRequest'
|
||||||
import common from '@ohos.app.ability.common';
|
import common from '@ohos.app.ability.common';
|
||||||
import { ImageKnife } from '../ImageKnife';
|
import { ImageKnife } from '../ImageKnife';
|
||||||
import { LogUtil } from '../utils/LogUtil';
|
import { LogUtil } from '../utils/LogUtil';
|
||||||
|
import { ImageKnifeRequestSource } from '../model/ImageKnifeData';
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
export struct ImageKnifeComponent {
|
export struct ImageKnifeComponent {
|
||||||
|
@ -24,6 +25,7 @@ export struct ImageKnifeComponent {
|
||||||
@State pixelMap: PixelMap | string | undefined = undefined
|
@State pixelMap: PixelMap | string | undefined = undefined
|
||||||
@State adaptiveWidth: Length = '100%'
|
@State adaptiveWidth: Length = '100%'
|
||||||
@State adaptiveHeight: Length = '100%'
|
@State adaptiveHeight: Length = '100%'
|
||||||
|
@State objectFit: ImageFit = ImageFit.Contain
|
||||||
private request: ImageKnifeRequest | undefined
|
private request: ImageKnifeRequest | undefined
|
||||||
private lastWidth: number = 0
|
private lastWidth: number = 0
|
||||||
private lastHeight: number = 0
|
private lastHeight: number = 0
|
||||||
|
@ -41,9 +43,8 @@ export struct ImageKnifeComponent {
|
||||||
// //画主图
|
// //画主图
|
||||||
// this.pixelMap = memoryCache.source;
|
// this.pixelMap = memoryCache.source;
|
||||||
// }else {
|
// }else {
|
||||||
LogUtil.log("aboutToAppear onLayoutComplete")
|
|
||||||
// }
|
// }
|
||||||
|
this.objectFit = this.imageKnifeOption.objectFit === undefined ? ImageFit.Contain : this.imageKnifeOption.objectFit
|
||||||
}
|
}
|
||||||
|
|
||||||
aboutToDisappear(): void {
|
aboutToDisappear(): void {
|
||||||
|
@ -58,11 +59,12 @@ export struct ImageKnifeComponent {
|
||||||
this.request.requestState = ImageKnifeRequestState.DESTROY
|
this.request.requestState = ImageKnifeRequestState.DESTROY
|
||||||
this.request = undefined
|
this.request = undefined
|
||||||
}
|
}
|
||||||
|
this.objectFit = this.imageKnifeOption.objectFit === undefined ? ImageFit.Contain : this.imageKnifeOption.objectFit
|
||||||
}
|
}
|
||||||
|
|
||||||
build() {
|
build() {
|
||||||
Image(this.pixelMap)
|
Image(this.pixelMap)
|
||||||
.objectFit(this.imageKnifeOption.objectFit === undefined ? ImageFit.Contain : this.imageKnifeOption.objectFit)
|
.objectFit(this.objectFit)
|
||||||
.width(this.adaptiveWidth)
|
.width(this.adaptiveWidth)
|
||||||
.height(this.adaptiveHeight)
|
.height(this.adaptiveHeight)
|
||||||
.border(this.imageKnifeOption.border)
|
.border(this.imageKnifeOption.border)
|
||||||
|
@ -108,7 +110,7 @@ export struct ImageKnifeComponent {
|
||||||
height,
|
height,
|
||||||
this.componentVersion,
|
this.componentVersion,
|
||||||
{
|
{
|
||||||
showPixelMap: async (version: number, pixelMap: PixelMap | string) => {
|
showPixelMap: async (version: number, pixelMap: PixelMap | string, requestSource: ImageKnifeRequestSource) => {
|
||||||
if (version !== this.componentVersion) {
|
if (version !== this.componentVersion) {
|
||||||
return //针对reuse场景,不显示历史图片
|
return //针对reuse场景,不显示历史图片
|
||||||
}
|
}
|
||||||
|
@ -132,6 +134,17 @@ export struct ImageKnifeComponent {
|
||||||
} else {
|
} else {
|
||||||
//console.info("KKKKKKKKKKK:" + pixelMap)
|
//console.info("KKKKKKKKKKK:" + pixelMap)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (requestSource == ImageKnifeRequestSource.SRC) {
|
||||||
|
this.objectFit =
|
||||||
|
this.imageKnifeOption.objectFit === undefined ? ImageFit.Contain : this.imageKnifeOption.objectFit
|
||||||
|
} else if (requestSource == ImageKnifeRequestSource.PLACE_HOLDER) {
|
||||||
|
this.objectFit =
|
||||||
|
this.imageKnifeOption.placeholderObjectFit === undefined ? (this.imageKnifeOption.objectFit === undefined ? ImageFit.Contain : this.imageKnifeOption.objectFit) : this.imageKnifeOption.placeholderObjectFit
|
||||||
|
} else {
|
||||||
|
this.objectFit =
|
||||||
|
this.imageKnifeOption.errorholderObjectFit === undefined ? (this.imageKnifeOption.objectFit === undefined ? ImageFit.Contain : this.imageKnifeOption.objectFit) : this.imageKnifeOption.errorholderObjectFit
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,26 +23,16 @@ export interface ImageKnifeData {
|
||||||
imageWidth: number,
|
imageWidth: number,
|
||||||
imageHeight: number
|
imageHeight: number
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 读取缓存策略
|
* 缓存策略
|
||||||
*/
|
*/
|
||||||
export enum ReadCacheStrategyType {
|
export enum CacheStrategy {
|
||||||
// 默认-读取内存和文件缓存
|
// 默认-写入/读取内存和文件缓存
|
||||||
Default = 0,
|
Default = 0,
|
||||||
// 只读取内存缓存
|
// 只写入/读取内存缓存
|
||||||
Memory = 1,
|
Memory = 1,
|
||||||
// 只读取文件缓存
|
// 只写入/读取文件缓存
|
||||||
File = 2
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 写入缓存策略
|
|
||||||
*/
|
|
||||||
export enum WriteCacheStrategyType {
|
|
||||||
// 默认-写入内存和文件缓存
|
|
||||||
Default = 0,
|
|
||||||
// 只写入内存缓存
|
|
||||||
Memory = 1,
|
|
||||||
// 只写入文件缓存
|
|
||||||
File = 2
|
File = 2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,7 +72,7 @@ export interface RequestJobRequest {
|
||||||
onlyRetrieveFromCache?: boolean
|
onlyRetrieveFromCache?: boolean
|
||||||
requestSource: ImageKnifeRequestSource
|
requestSource: ImageKnifeRequestSource
|
||||||
transformation?: PixelMapTransformation
|
transformation?: PixelMapTransformation
|
||||||
writeCacheStrategy?: WriteCacheStrategyType
|
writeCacheStrategy?: CacheStrategy
|
||||||
signature?: string
|
signature?: string
|
||||||
engineKey: IEngineKey
|
engineKey: IEngineKey
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue