import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { image } from '@kit.ImageKit';
import { fileIo as fs } from '@kit.CoreFileKit';@Entry
@Component
struct Index {@State getAlbum: string = '显示相册中的图片';@State pixelsArray : Array<image.PixelMap> | undefined = undefined;@State albumPath: Array<string> = [];@State photoSize: number = 0;async getPictureFromAlbum() {// 拉起相册,选择图片let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;PhotoSelectOptions.maxSelectNumber = 3;let photoPicker = new photoAccessHelper.PhotoViewPicker();let photoSelectResult: photoAccessHelper.PhotoSelectResult = await photoPicker.select(PhotoSelectOptions);photoSelectResult.photoUris.forEach(item => {this.albumPath.push(item);});// 读取图片为 bufferconst promises = this.albumPath.map(async (path) => {const file = fs.openSync(path, fs.OpenMode.READ_ONLY);this.photoSize = fs.statSync(file.fd).size;console.info('Photo Size: ' + this.photoSize);let buffer = new ArrayBuffer(this.photoSize);fs.readSync(file.fd, buffer);fs.closeSync(file);// 解码成 PixelMapconst imageSource = image.createImageSource(buffer);return await imageSource.createPixelMap({});});this.pixelsArray = await Promise.all(promises);}build() {Row() {Column() {Button('显示照片').onClick(() => {this.getPictureFromAlbum();})Grid(){ForEach(this.pixelsArray,(item:image.PixelMap,index:number)=>{GridItem(){Image(item).width('100%').aspectRatio(1)}})}.width('100%').columnsTemplate('1fr 1fr 1fr')}.width('100%')}.height('100%')}
}
PhotoSelectOptions.maxSelectNumber = 3; 要上传的图片数量