Action script如何复制一个Bitmap对象

本文探讨了在Flash中复制Bitmap对象的有效方法。针对常见的应用场景,对比了三种不同的复制方案,并详细解析了每种方案的优缺点及实现细节。

本人最近遇到需要复制一个Bitmap对象的问题,应用场景如下:

在我的一个Flash中经常需要加载同样的图片,而这张图片也是比较大的,所以就想第一次使用后就把这张图片缓存起来,以后用时直接从缓存读取就可以了,因此就遇到Bitmap对象的复制问题,在网上搜了一下,网友们有两种解决方案,如下
方案1:

Title public class MyImage{
  public var bitmap: Bitmap;
  public var url: String;
  public var x: number;
  public var y: number;

  public function clone(): MyImage()
  {
      var myImage: MyImage = new MyImage();
      myImage.bitmap = this.bitmap.clone;
      myImage.url = this.url;
      myImage.x = this.x;
      myImage.y = this.y;
      return myImage;
  }
}

这种方案网上有人说可以,但是我自己试了一下,好像不行,“this.bitmap.clone”这句话不对,Bitmap就没有clone这个属性
方案2:

...

import flash.net.registerClassAlias;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;

...

public function clone(source:Object) :* {
        var typeName:String = getQualifiedClassName(source);//获取全名
        var packageName:String = typeName.split("::")[1];//切出包名
        var type:Class = Class(getDefinitionByName(typeName));//获取Class

        registerClassAlias(packageName, type);//注册Class
       
        //复制对象
        var copier:ByteArray = new ByteArray ();
        copier.writeObject(source);
        copier.position = 0;
        return copier.readObject();
}

这种方案虽然不报错,对象也复制成功了,但是好像图片就是无法正常显示
方案3:这是我自己的方案,其实也是很简单的事情

new Bitmap(sourceBitmap.bitMapData.clone())

Bitmap的bitmapData有clone的方法
这个方法我试过,可以正常使用

<!-- pages/webview/webview.vue --> <template> <view class="webview-container"> <cover-view class="webview-wrapper"> <web-view :src="webviewPath" id="targetWebview" ></web-view> </cover-view> <cover-view class="bottom-action-area"> <cover-view class="capture-btn" @click="captureWebview"> <cover-view class="btn-text">立即取证</cover-view> </cover-view> </cover-view> <view wx:if="{{show}}" class="custom-preview"> <image src="{{current}}" mode="aspectFit" bindtap="hideMenu"/> <view class="menu-bar" wx:if="{{showMenu}}"> <button bindtap="saveImage">放弃</button> <button bindtap="shareImage">固化</button> </view> </view> </view> </template> <script setup lang="ts"> import { onLoad,onReady } from '@dcloudio/uni-app'; import { ref, computed, watchEffect } from 'vue'; // 声明全局的 plus 对象 declare const plus: any; // 接收传递的URL参数 const image = ref(''); const webviewPath = ref(''); const ws = ref<any>(null);// WebView实例 const bitmap = ref<any>(null); const screenshotPath = ref(''); // 存储截图路径 const originalHeight = ref(0); // 原始高度 const isCapturing = ref(false); // 防止重复点击 // 页面加载时获取参数 onLoad((options: any) => { if (options.url) { webviewPath.value = decodeURIComponent(options.url); console.log('加载WebView:', webviewPath.value); } }); // 获取系统窗口高度 const windowHeight = uni.getSystemInfoSync().windowHeight; originalHeight.value = windowHeight; onReady(() => { // 获取当前页面实例 const pages = getCurrentPages(); const currentPage = pages[pages.length - 1]; // 获取当前页面的原生Webview对象 const currentWebview = currentPage.$getAppWebview(); setTimeout(() => { if (currentWebview.children().length > 0) { const wv = currentWebview.children()[0]; ws.value = wv; console.log('WebView实例已获取', ws.value); // 设置Webview的高度(如果需要长截图) wv.setStyle({ height: '100%' }); } }, 1000); }); const captureWebview = async () => { if (!ws.value) { uni.showToast({ title: 'WebView未准备好', icon: 'none' }); return; } // 显示加载提示 uni.showLoading({ title: '截图中...', mask: true }); // 创建位图对象 const bitmap = new plus.nativeObj.Bitmap('screenshot'); // 截图操作 ws.value.draw(bitmap, () => { const filePath = `_doc/${Date.now()}.jpg`; bitmap.save(filePath, {}, (res: any) => { screenshotPath.value = res.target; // 保存到相册 uni.saveImageToPhotosAlbum({ filePath: screenshotPath.value, success: () => { uni.showToast({ title: '截图保存成功' }); setTimeout(() => { // 预览图片 uni.previewImage({ current: screenshotPath.value, urls:[screenshotPath.value], longPressActions: { itemList: ['放弃', '固化'], success: function(data) { console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片'); }, fail: function(err) { console.log(err.errMsg); } } }); }, 300); }, fail: (err) => { uni.showToast({ title: '保存失败', icon: 'none' }); console.error(err); } }); // 清除Bitmap对象 bitmap.clear(); }); }, (err: any) => { uni.showToast({ title: '截图失败', icon: 'none' }); console.error(err); }); } </script> <style> .webview-container { position: relative; width: 100%; height: 100vh; overflow: hidden; } .webview-wrapper { /* 减去底部按钮高度,避免覆盖 */ height: calc(100vh - 120rpx); width: 100%; overflow: hidden; } .bottom-action-area { position: fixed; bottom: 0; left: 0; right: 0; z-index: 100; /* 确保在 web-view 上方 */ background-color: #007aff; padding: 30rpx; padding-bottom: constant(safe-area-inset-bottom); padding-bottom: env(safe-area-inset-bottom); display: flex; justify-content: center; align-items: center; } /* 其他样式保持不变 */ .capture-btn { width: 100%; height: 90rpx; background-color: #007aff; border-radius: 45rpx; box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.2); display: flex; align-items: center; justify-content: center; } .btn-text { color: #ffffff; font-size: 32rpx; font-weight: 500; } </style> 保留自定义预览组件 改为预览的时候就显示放弃和固化
08-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值