使用Image组件加载图片时,图片自动被旋转了90度,如何使图片显示方向正确。问题代码如下:
@Entry
@Component
struct ImageRotationProblem {
build() {
Column() {
// 运行时请按需替换图片资源
Image($r('app.media.startIcon'));
};
}
} 左图为正常显示效果,右图为Image加载显示效果:

方案一:通过组件旋转属性,使图片显示方向正常:
直接给Image组件加上对应的旋转属性,使图片正常显示。因为图片的方向是逆时针90度,将Image组件顺时针旋转90度后,图片会正常显示。通过以下属性旋转:
@Entry
@Component
struct ImageRotationOne {
build() {
Column() {
// 运行时请按需替换图片资源
Image($r('app.media.startIcon'))
.rotate({ angle: 90 });
};
}
}
这种方案只有在确定图片的方向信息时才能使用,如果是从网络加载的、不能确定方向的图片列表,该方案则不适用。
方案二:通过orientation属性,设置图片的显示方向:
利用orientation属性,设置图像内容的显示方向。如果需要显示携带旋转角度信息或翻转信息的图片建议使用ImageRotateOrientation.AUTO进行设置。示例代码如下:
// 运行时请按需替换图片资源
@Entry
@Component
struct ImageRotationTwo {
build() {
Column() {
// 运行时请按需替换图片资源
Image($r('app.media.startIcon'))
.orientation(ImageRotateOrientation.AUTO);
};
}
}
这种方案不支持gif和svg类型的图片。
方案三:将图片转成ImageSource对象后读取旋转信息:
思路:读取每张图片的方向orientation信息,根据其方向,设置图片的显示方向。
详细代码可以参考:获取图片的Exif信息并设置图像内容的显示方向。
@Entry
@Component
struct ImgExample {
build() {
Stack() {
Image($r('app.media.long')) // $r('app.media.long')需要替换为开发者需要的图片资源文件
.width(200)
.height(300)
.objectFit(ImageFit.Auto)
// 没有使用linearGradient设置颜色渐变的同层组件
}
.width('100%')
.height('100%');
}
} 添加使用linearGradient设置颜色渐变的同层组件。
@Entry
@Component
struct ImgExample {
build() {
Stack() {
Stack() {
Image($r('app.media.long')) // $r('app.media.long')需要替换为开发者需要的图片资源文件
.width('100%')
.height('100%')
.objectFit(ImageFit.Auto);
// 使用linearGradient设置颜色渐变的同层组件
Row()
.width('100%')
.height('40')
.linearGradient({
direction: GradientDirection.Bottom,
colors: [['#00000000', 0.0], ['#ff000000', 1.0]]
});
}
.width(200)
.height(300)
.alignContent(Alignment.Bottom);
}
.width('100%')
.height('100%');
}
} 效果图如下:

@Entry
@Component
struct ImgEmpty {
build() {
Stack() {
Image($r('app.media.product')) // $r('app.media.product')需要替换为开发者需要的图片资源文件
.width(300)
.height(200)
.objectFit(ImageFit.Cover) // 图片保持宽高比并填满容器,超出部分会被裁剪
}
.width('100%')
.height('100%');
}
} objectFit属性设置为Contain,该缩放模式会保持宽高比,完整显示图片。
@Entry
@Component
struct ImgTruncation {
build() {
Stack() {
Image($r('app.media.product')) // $r('app.media.product')需要替换为开发者需要的图片资源文件
.width(300)
.height(200)
.objectFit(ImageFit.Contain) // 保持宽高比,完整显示图片
}
.width('100%')
.height('100%');
}
} @Entry
@Component
struct ImgEmpty {
imgSrcs: ResourceStr[] = ['', $r('app.media.myposter2')];
build() {
Stack() {
List() {
ForEach(this.imgSrcs, (img: ResourceStr, index: number) => {
ListItem() {
Row() {
Text('图片' + (index + 1))
.fontSize(20)
.height('100%')
.width(100)
.textAlign(TextAlign.Center)
.borderRadius(10);
// 图片资源为空时不显示图片组件
if (img !== '') {
Image(img)
.width(150)
.objectFit(ImageFit.Contain);
}
}
.width('100%')
.height(120)
.justifyContent(FlexAlign.Start)
.borderRadius(10)
.backgroundColor('#f1f3f5');
}
.margin({ top: 5, bottom: 5 });
}, (item: string) => item);
}
.nestedScroll({
scrollForward: NestedScrollMode.PARENT_FIRST,
scrollBackward: NestedScrollMode.SELF_FIRST
})
.scrollBar(BarState.Off)
.width('80%');
}
.width('100%')
.height('100%');
}
} 无图片资源时通过alt属性使用占位图片替代。
@Entry
@Component
struct ImgEmpty {
imgSrcs: ResourceStr[] = ['', $r('app.media.startIcon')]; // $r('app.media.startIcon')需要替换为开发者需要的图片资源文件
build() {
Stack() {
List() {
ForEach(this.imgSrcs, (img: ResourceStr, index: number) => {
ListItem() {
Row() {
Text('图片' + (index + 1))
.fontSize(20)
.height('100%')
.width(100)
.textAlign(TextAlign.Center)
.borderRadius(10);
Image(img)
.width(100)
// $r('app.media.empty')需要替换为开发者需要的图片资源文件
.alt($r('app.media.empty')) // 图片资源为空时显示占位图片
.objectFit(ImageFit.Contain);
}
.width('100%')
.height(120)
.justifyContent(FlexAlign.Start)
.borderRadius(10)
.backgroundColor('#f1f3f5');
}
.margin({ top: 5, bottom: 5 });
}, (item: string) => item);
}
.nestedScroll({
scrollForward: NestedScrollMode.PARENT_FIRST,
scrollBackward: NestedScrollMode.SELF_FIRST
})
.scrollBar(BarState.Off)
.width('80%');
}
.width('100%')
.height('100%');
}
} 效果图如下:
